Project: imagemagick (imagemagick/imagemagick), language: c++
Vulnerability instance: imagemagick.cve-2017-17884
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/902
## Description:
Issue: ImageMagick/ImageMagick#902
Title: memory leaks in WriteOnePNGImage
State: closed
Created by: henices
Created at: 2017-12-19 07:22:34+00:00
Labels: bug

Issue Body:
/usr/local/bin/magick -version
Version: ImageMagick 7.0.7-16 Q16 x86_64 2017-12-19 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP 
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gvc jbig jng jpeg lcms lqr lzma pangocairo png rsvg tiff webp wmf x xml zlib

Trigger Command: **magick convert 'memory-leaks-CySdxTUBH3AFFf8fpXeyNzsjxMbxmLvm.svg~' /dev/null**

```
convert: cache resources exhausted `' @ error/cache.c/OpenPixelCache/3655.
convert: memory allocation failed `' @ error/png.c/WriteOnePNGImage/9256.

=================================================================
==22135==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 13488 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940af94a2 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7fd940afd9a6 in CloneImage MagickCore/image.c:826
    #4 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #5 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #6 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #7 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #8 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #9 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #10 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #11 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #12 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #13 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #14 0x401b16 in MagickMain utilities/magick.c:149
    #15 0x401d80 in main utilities/magick.c:180
    #16 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Direct leak of 13024 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940af94a2 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7fd940afbe4f in AcquireImageInfo MagickCore/image.c:346
    #4 0x7fd940afef90 in CloneImageInfo MagickCore/image.c:947
    #5 0x7fd940ff3311 in WriteOnePNGImage coders/png.c:8440
    #6 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #7 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #8 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #9 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #10 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #11 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #12 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #13 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #14 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #15 0x401b16 in MagickMain utilities/magick.c:149
    #16 0x401d80 in main utilities/magick.c:180
    #17 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 89472 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940b3e7dd in AcquireAlignedMemory MagickCore/memory.c:262
    #2 0x7fd940909245 in AcquireCacheNexusPixels MagickCore/cache.c:4724
    #3 0x7fd940909e54 in SetPixelCacheNexusPixels MagickCore/cache.c:4844
    #4 0x7fd940906066 in QueueAuthenticPixelCacheNexus MagickCore/cache.c:3967
    #5 0x7fd9408f9cdb in GetAuthenticPixelCacheNexus MagickCore/cache.c:1236
    #6 0x7fd9408fa675 in GetAuthenticPixels MagickCore/cache.c:1407
    #7 0x7fd940ff77f0 in WriteOnePNGImage coders/png.c:8973
    #8 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #9 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #10 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #11 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #12 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #13 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #14 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #15 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #16 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #17 0x401b16 in MagickMain utilities/magick.c:149
    #18 0x401d80 in main utilities/magick.c:180
    #19 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 65536 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940b3e931 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7fd9408e37e2 in ImageToBlob MagickCore/blob.c:1898
    #4 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #5 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #6 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #7 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #8 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #9 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #10 0x401b16 in MagickMain utilities/magick.c:149
    #11 0x401d80 in main utilities/magick.c:180
    #12 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 22616 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940b3e931 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7fd94092c151 in AcquireImageColormap MagickCore/colormap.c:119
    #4 0x7fd940ff94b9 in WriteOnePNGImage coders/png.c:9253
    #5 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #6 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #7 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #8 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #9 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #10 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #11 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #12 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #13 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #14 0x401b16 in MagickMain utilities/magick.c:149
    #15 0x401d80 in main utilities/magick.c:180
    #16 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 13024 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940af94a2 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7fd940afbe4f in AcquireImageInfo MagickCore/image.c:346
    #4 0x7fd940afef90 in CloneImageInfo MagickCore/image.c:947
    #5 0x7fd940afdf2d in CloneImage MagickCore/image.c:840
    #6 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #7 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #8 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #9 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #10 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #11 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #12 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #13 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #14 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #15 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #16 0x401b16 in MagickMain utilities/magick.c:149
    #17 0x401d80 in main utilities/magick.c:180
    #18 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 9096 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd9408f5258 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7fd9408f5381 in AcquirePixelCache MagickCore/cache.c:192
    #4 0x7fd9408f6219 in ClonePixelCache MagickCore/cache.c:411
    #5 0x7fd9408fb77f in GetImagePixelCache MagickCore/cache.c:1632
    #6 0x7fd940905cc5 in QueueAuthenticPixelCacheNexus MagickCore/cache.c:3941
    #7 0x7fd9408f9cdb in GetAuthenticPixelCacheNexus MagickCore/cache.c:1236
    #8 0x7fd9408fa675 in GetAuthenticPixels MagickCore/cache.c:1407
    #9 0x7fd940ff77f0 in WriteOnePNGImage coders/png.c:8973
    #10 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #11 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #12 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #13 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #14 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #15 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #16 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #17 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #18 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #19 0x401b16 in MagickMain utilities/magick.c:149
    #20 0x401d80 in main utilities/magick.c:180
    #21 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 704 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940b3e931 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7fd9408f5ae7 in AcquirePixelCacheNexus MagickCore/cache.c:263
    #4 0x7fd9408f569e in AcquirePixelCache MagickCore/cache.c:206
    #5 0x7fd9408f6219 in ClonePixelCache MagickCore/cache.c:411
    #6 0x7fd9408fb77f in GetImagePixelCache MagickCore/cache.c:1632
    #7 0x7fd940905cc5 in QueueAuthenticPixelCacheNexus MagickCore/cache.c:3941
    #8 0x7fd9408f9cdb in GetAuthenticPixelCacheNexus MagickCore/cache.c:1236
    #9 0x7fd9408fa675 in GetAuthenticPixels MagickCore/cache.c:1407
    #10 0x7fd940ff77f0 in WriteOnePNGImage coders/png.c:8973
    #11 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #12 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #13 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #14 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #15 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #16 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #17 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #18 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #19 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #20 0x401b16 in MagickMain utilities/magick.c:149
    #21 0x401d80 in main utilities/magick.c:180
    #22 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 512 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940b3e931 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7fd940b7461c in AcquirePixelChannelMap MagickCore/pixel.c:101
    #4 0x7fd940b747ad in ClonePixelChannelMap MagickCore/pixel.c:139
    #5 0x7fd940afe1f0 in CloneImage MagickCore/image.c:851
    #6 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #7 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #8 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #9 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #10 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #11 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #12 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #13 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #14 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #15 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #16 0x401b16 in MagickMain utilities/magick.c:149
    #17 0x401d80 in main utilities/magick.c:180
    #18 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 280 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd9408dc027 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7fd9408dd1b2 in CloneBlobInfo MagickCore/blob.c:542
    #4 0x7fd940afe58c in CloneImage MagickCore/image.c:869
    #5 0x7fd940f56b9b in TraceSVGImage coders/svg.c:3633
    #6 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #7 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #8 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #9 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #10 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x401b16 in MagickMain utilities/magick.c:149
    #12 0x401d80 in main utilities/magick.c:180
    #13 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940c4cb0e in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7fd940c50a36 in NewSplayTree MagickCore/splay-tree.c:1148
    #4 0x7fd940c4dbf6 in CloneSplayTree MagickCore/splay-tree.c:360
    #5 0x7fd940ba5582 in CloneImageProperties MagickCore/property.c:196
    #6 0x7fd940afdf88 in CloneImage MagickCore/image.c:842
    #7 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #8 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #9 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #10 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #11 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #12 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #13 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #14 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #15 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #16 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #17 0x401b16 in MagickMain utilities/magick.c:149
    #18 0x401d80 in main utilities/magick.c:180
    #19 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940c3926e in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7fd940c39358 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7fd940afe68c in CloneImage MagickCore/image.c:873
    #4 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #5 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #6 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #7 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #8 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #9 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #10 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #11 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #12 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #13 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #14 0x401b16 in MagickMain utilities/magick.c:149
    #15 0x401d80 in main utilities/magick.c:180
    #16 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940c3926e in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7fd940c39358 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7fd940c50c27 in NewSplayTree MagickCore/splay-tree.c:1159
    #4 0x7fd940c4dbf6 in CloneSplayTree MagickCore/splay-tree.c:360
    #5 0x7fd940ba5582 in CloneImageProperties MagickCore/property.c:196
    #6 0x7fd940afdf88 in CloneImage MagickCore/image.c:842
    #7 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #8 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #9 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #10 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #11 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #12 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #13 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #14 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #15 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #16 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #17 0x401b16 in MagickMain utilities/magick.c:149
    #18 0x401d80 in main utilities/magick.c:180
    #19 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 2 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940c4d047 in AddValueToSplayTree MagickCore/splay-tree.c:189
    #3 0x7fd940c4dd7f in CloneSplayTree MagickCore/splay-tree.c:372
    #4 0x7fd940ba5582 in CloneImageProperties MagickCore/property.c:196
    #5 0x7fd940afdf88 in CloneImage MagickCore/image.c:842
    #6 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #7 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #8 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #9 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #10 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #11 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #12 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #13 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #14 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #15 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #16 0x401b16 in MagickMain utilities/magick.c:149
    #17 0x401d80 in main utilities/magick.c:180
    #18 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940c3926e in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7fd940c39358 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7fd9408f5917 in AcquirePixelCache MagickCore/cache.c:223
    #4 0x7fd9408f6219 in ClonePixelCache MagickCore/cache.c:411
    #5 0x7fd9408fb77f in GetImagePixelCache MagickCore/cache.c:1632
    #6 0x7fd940905cc5 in QueueAuthenticPixelCacheNexus MagickCore/cache.c:3941
    #7 0x7fd9408f9cdb in GetAuthenticPixelCacheNexus MagickCore/cache.c:1236
    #8 0x7fd9408fa675 in GetAuthenticPixels MagickCore/cache.c:1407
    #9 0x7fd940ff77f0 in WriteOnePNGImage coders/png.c:8973
    #10 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #11 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #12 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #13 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #14 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #15 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #16 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #17 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #18 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #19 0x401b16 in MagickMain utilities/magick.c:149
    #20 0x401d80 in main utilities/magick.c:180
    #21 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940c3926e in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7fd940c39358 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7fd9408e2911 in GetBlobInfo MagickCore/blob.c:1628
    #4 0x7fd9408dd1c2 in CloneBlobInfo MagickCore/blob.c:543
    #5 0x7fd940afe58c in CloneImage MagickCore/image.c:869
    #6 0x7fd940f56b9b in TraceSVGImage coders/svg.c:3633
    #7 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #8 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #9 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #10 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #11 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x401b16 in MagickMain utilities/magick.c:149
    #13 0x401d80 in main utilities/magick.c:180
    #14 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940c3926e in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7fd940c39358 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7fd9408f58a7 in AcquirePixelCache MagickCore/cache.c:221
    #4 0x7fd9408f6219 in ClonePixelCache MagickCore/cache.c:411
    #5 0x7fd9408fb77f in GetImagePixelCache MagickCore/cache.c:1632
    #6 0x7fd940905cc5 in QueueAuthenticPixelCacheNexus MagickCore/cache.c:3941
    #7 0x7fd9408f9cdb in GetAuthenticPixelCacheNexus MagickCore/cache.c:1236
    #8 0x7fd9408fa675 in GetAuthenticPixels MagickCore/cache.c:1407
    #9 0x7fd940ff77f0 in WriteOnePNGImage coders/png.c:8973
    #10 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #11 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #12 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #13 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #14 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #15 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #16 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #17 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #18 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #19 0x401b16 in MagickMain utilities/magick.c:149
    #20 0x401d80 in main utilities/magick.c:180
    #21 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7fd9417674a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7fd940b3e7dd in AcquireAlignedMemory MagickCore/memory.c:262
    #2 0x7fd9408f59f6 in AcquirePixelCacheNexus MagickCore/cache.c:259
    #3 0x7fd9408f569e in AcquirePixelCache MagickCore/cache.c:206
    #4 0x7fd9408f6219 in ClonePixelCache MagickCore/cache.c:411
    #5 0x7fd9408fb77f in GetImagePixelCache MagickCore/cache.c:1632
    #6 0x7fd940905cc5 in QueueAuthenticPixelCacheNexus MagickCore/cache.c:3941
    #7 0x7fd9408f9cdb in GetAuthenticPixelCacheNexus MagickCore/cache.c:1236
    #8 0x7fd9408fa675 in GetAuthenticPixels MagickCore/cache.c:1407
    #9 0x7fd940ff77f0 in WriteOnePNGImage coders/png.c:8973
    #10 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #11 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #12 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #13 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #14 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #15 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #16 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #17 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #18 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #19 0x401b16 in MagickMain utilities/magick.c:149
    #20 0x401d80 in main utilities/magick.c:180
    #21 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 52 byte(s) in 2 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940b3e931 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7fd940c77f34 in ConstantString MagickCore/string.c:700
    #4 0x7fd940c4dd13 in CloneSplayTree MagickCore/splay-tree.c:373
    #5 0x7fd940ba5582 in CloneImageProperties MagickCore/property.c:196
    #6 0x7fd940afdf88 in CloneImage MagickCore/image.c:842
    #7 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #8 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #9 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #10 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #11 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #12 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #13 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #14 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #15 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #16 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #17 0x401b16 in MagickMain utilities/magick.c:149
    #18 0x401d80 in main utilities/magick.c:180
    #19 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

Indirect leak of 24 byte(s) in 2 object(s) allocated from:
    #0 0x7fd941766850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7fd940b3e8dd in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7fd940b3e931 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7fd940c77f34 in ConstantString MagickCore/string.c:700
    #4 0x7fd940c4dd6a in CloneSplayTree MagickCore/splay-tree.c:372
    #5 0x7fd940ba5582 in CloneImageProperties MagickCore/property.c:196
    #6 0x7fd940afdf88 in CloneImage MagickCore/image.c:842
    #7 0x7fd940ff32dd in WriteOnePNGImage coders/png.c:8437
    #8 0x7fd94100ca95 in WritePNGImage coders/png.c:12676
    #9 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #10 0x7fd9408e396a in ImageToBlob MagickCore/blob.c:1908
    #11 0x7fd940f56c3d in TraceSVGImage coders/svg.c:3639
    #12 0x7fd940f5751f in WriteSVGImage coders/svg.c:3749
    #13 0x7fd94096f3cd in WriteImage MagickCore/constitute.c:1114
    #14 0x7fd94097010a in WriteImages MagickCore/constitute.c:1333
    #15 0x7fd940198d6f in ConvertImageCommand MagickWand/convert.c:3280
    #16 0x7fd94029d25c in MagickCommandGenesis MagickWand/mogrify.c:183
    #17 0x401b16 in MagickMain utilities/magick.c:149
    #18 0x401d80 in main utilities/magick.c:180
    #19 0x7fd939e85039 in __libc_start_main (/lib64/libc.so.6+0x21039)

SUMMARY: AddressSanitizer: 228364 byte(s) leaked in 23 allocation(s).
```
[testcase](https://raw.githubusercontent.com/henices/pocs/master/memory-leaks-CySdxTUBH3AFFf8fpXeyNzsjxMbxmLvm.svg~)

Credit: NSFocus Security Team <security (at) nsfocus (dot) com>


Comments:
Comment by urban-warrior on 2017-12-19 18:58:46+00:00:
Thanks for the problem report.  We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today.  The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow. 


---

Comment by nohmask on 2017-12-25 04:04:11+00:00:
This was assigned CVE-2017-17884.

---

Comment by pgajdos on 2018-01-16 14:21:26+00:00:
Hi @dlemstra,

the testcase and command shows several leaks detected by valgrind for me with version 7.0.7-11 and 6.9.9-33, does it for you as well?

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
./configure --prefix="$WORK" --disable-shared --disable-docs
make "-j$(nproc)"
make install -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool
RUN git clone https://github.com/imagemagick/imagemagick imagemagick
RUN git -C imagemagick checkout ddbf0ed10323d0fb329189041190630a8f7af5a7
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files:
filename: crop_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  uint16_t Width;
  uint16_t Height;
  if (Size < (sizeof(Width) + sizeof(Height))) {
    return 0;
  }
  Width = *reinterpret_cast<const uint16_t *>(Data);
  Height = *reinterpret_cast<const uint16_t *>(Data + sizeof(Width));
  const Magick::Blob blob(Data + sizeof(Width) + sizeof(Height),
                          Size - (sizeof(Width) + sizeof(Height)));
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }
  image.crop(Magick::Geometry(Width, Height));
  return 0;
}

filename: encoder_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

#define FUZZ_ENCODER_STRING_LITERAL(name) #name
#define FUZZ_ENCODER FUZZ_ENCODER_STRING_LITERAL(FUZZ_IMAGEMAGICK_ENCODER)

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }

  Magick::Blob outBlob;
  try {
    image.write(&outBlob, FUZZ_ENCODER);
  } catch (Magick::Exception &e) {
  }
  return 0;
}

filename: encoder_list.cc
content: #include <iostream>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int main() {
  size_t nFormats;
  Magick::ExceptionInfo ex;
  const Magick::MagickInfo **formats = GetMagickInfoList("*", &nFormats, &ex);

  for (size_t i = 0; i < nFormats; i++) {
    const Magick::MagickInfo *format = formats[i];
    if (format->encoder && format->name) {
      std::cout << format->name << std::endl;
    }
  }
}

filename: enhance_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  try {
    image.read(blob);
    image.enhance();
  } catch (Magick::Exception &e) {
    return 0;
  }
  return 0;
}

filename: huffman_decode_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }
  Magick::ExceptionInfo ex;
  auto res = HuffmanDecodeImage(image.image(), &ex);
  return 0;
}

filename: rotate_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  if (Size < sizeof(double)) {
    return 0;
  }
  double Degrees = *reinterpret_cast<const double *>(Data);
  if (!isfinite(Degrees)) {
    return 0;
  }
  const Magick::Blob blob(Data + sizeof(Degrees), Size - sizeof(Degrees));
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }
  image.rotate(Degrees);
  return 0;
}