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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/2889
## Description:
Issue: ImageMagick/ImageMagick#2889
Title: Memory leak in identify -help
State: closed
Created by: luisfrod
Created at: 2020-11-30 15:25:48+00:00

Issue Body:
### Prerequisites

- [x] I have written a descriptive issue title
- [x] I have verified that I am using the latest version of ImageMagick
- [x] I have searched [open](https://github.com/ImageMagick/ImageMagick/issues) and [closed](https://github.com/ImageMagick/ImageMagick/issues?q=is%3Aissue+is%3Aclosed) issues to ensure it has not already been reported

### Description
```
==15438==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 13024 byte(s) in 1 object(s) allocated from:
    #0 0x494bad in malloc (/usr/local/bin/magick+0x494bad)
    #1 0x7f5ee70c0a35 in AcquireMagickMemory MagickCore/memory.c:531:10
    #2 0x7f5ee70c0a4b in AcquireCriticalMemory MagickCore/memory.c:607:10
    #3 0x7f5ee7088a9f in AcquireImageInfo MagickCore/image.c:328:28
    #4 0x7f5ee708ef55 in CloneImageInfo MagickCore/image.c:924:14
    #5 0x7f5ee6b4ff84 in IdentifyImageCommand MagickWand/identify.c:249:3
    #6 0x7f5ee6b970f0 in MagickCommandGenesis MagickWand/mogrify.c:191:14
    #7 0x4c45ad in MagickMain utilities/magick.c:149:10
    #8 0x4c42a8 in main utilities/magick.c:180:10
    #9 0x7f5ee62070b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x494bad in malloc (/usr/local/bin/magick+0x494bad)
    #1 0x7f5ee70c0a35 in AcquireMagickMemory MagickCore/memory.c:531:10
    #2 0x7f5ee70c0c50 in AcquireQuantumMemory MagickCore/memory.c:649:10
    #3 0x7f5ee71e7d3e in ExpandFilenames MagickCore/utility.c:771:20
    #4 0x7f5ee6b4ffd9 in IdentifyImageCommand MagickWand/identify.c:258:10
    #5 0x7f5ee6b970f0 in MagickCommandGenesis MagickWand/mogrify.c:191:14
    #6 0x4c45ad in MagickMain utilities/magick.c:149:10
    #7 0x4c42a8 in main magick.c:180:10
    #8 0x7f5ee62070b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

Indirect leak of 15 byte(s) in 2 object(s) allocated from:
    #0 0x494bad in malloc (/usr/local/bin/magick+0x494bad)
    #1 0x7f5ee70c0a35 in AcquireMagickMemory MagickCore/memory.c:531:10
    #2 0x7f5ee70c0c50 in AcquireQuantumMemory MagickCore/memory.c:649:10
    #3 0x7f5ee71b8570 in ConstantString MagickCore/string.c:679:26
    #4 0x7f5ee71e7fad in ExpandFilenames MagickCore/utility.c:799:21
    #5 0x7f5ee6b4ffd9 in IdentifyImageCommand MagickWand/identify.c:258:10
    #6 0x7f5ee6b970f0 in MagickCommandGenesis MagickWand/mogrify.c:191:14
    #7 0x4c45ad in MagickMain utilities/magick.c:149:10
    #8 0x4c42a8 in main utilities/magick.c:180:10
    #9 0x7f5ee62070b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

SUMMARY: AddressSanitizer: 13063 byte(s) leaked in 4 allocation(s).`
```

### Steps to Reproduce
Just the help argument of the identify command:
 ```
identify -help
```

### System Configuration
<!-- Tell us about the environment where you are experiencing the bug -->

- ImageMagick version:  ImageMagick 7.0.10-45 Q16 x86_64
- Environment (Operating system, version and so on): Ubuntu 20.04.1 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)
- Additional information: ./configure CC="clang" CFLAGS="-fsanitize=address -O1 -fno-omit-frame-pointer -g"

<!-- Thanks for reporting the issue to ImageMagick! -->



Comments:
Comment by dlemstra on 2020-11-30 15:56:48+00:00:
Thanks for the problem report.  We can reproduce it and will have a patch to fix it in the GIT master branch @ https://github.com/ImageMagick/ImageMagick later this week.  The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime next week. 

Build script:
#!/bin/bash
# Default build script template for C/C++ projects
set -eu

# Define configuration options
CONFIG_OPTS="--disable-shared"
CMAKE_OPTS="-DCMAKE_BUILD_TYPE=Debug"
MAKE_OPTS="-j$(nproc)"
BUILD_DIR="build"

# 1. Run build preparation scripts if they exist
for prep_script in buildconf autogen.sh bootstrap; do
  if [ -x "$prep_script" ]; then
    echo "Running $prep_script..."
    ./$prep_script
    break
  elif [ -f "$prep_script" ]; then
    echo "Found $prep_script but it's not executable, setting permissions..."
    chmod +x $prep_script
    ./$prep_script
    break
  fi
done

# 2. Handle different build systems
if [ -f "configure" ]; then
  echo "Found configure script, running with options: $CONFIG_OPTS"
  ./configure $CONFIG_OPTS

  echo "Building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -f "CMakeLists.txt" ]; then
  echo "Found CMakeLists.txt, using CMake build system"
  mkdir -p $BUILD_DIR
  cd $BUILD_DIR
  cmake $CMAKE_OPTS ..
  make $MAKE_OPTS
elif [ -f "Makefile" ] || [ -f "makefile" ]; then
  echo "Found Makefile, building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -f "meson.build" ]; then
  echo "Found meson.build, using Meson build system"
  mkdir -p $BUILD_DIR
  meson setup $BUILD_DIR .
  cd $BUILD_DIR
  ninja
elif [ -f "GNUmakefile" ]; then
  echo "Found GNUmakefile, building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -d ".git" ] && [ -f "Makefile.am" ]; then
  echo "Found Makefile.am, running autoreconf"
  autoreconf -i
  ./configure $CONFIG_OPTS
  make $MAKE_OPTS
else
  echo "WARNING: No recognized build system found!"
  echo "Trying generic approach:"
  
  # Try to detect common source patterns and compile them
  if ls *.c >/dev/null 2>&1 || ls *.cpp >/dev/null 2>&1 || ls *.cc >/dev/null 2>&1; then
    echo "Found C/C++ source files, attempting direct compilation"
    if ls *.cpp >/dev/null 2>&1 || ls *.cc >/dev/null 2>&1; then
      CXX=${CXX:-g++}
      echo "Compiling C++ sources with $CXX"
      $CXX -g -O1 -fsanitize=address -fno-omit-frame-pointer -o main *.cpp *.cc 2>/dev/null || true
    elif ls *.c >/dev/null 2>&1; then
      CC=${CC:-gcc}
      echo "Compiling C sources with $CC"
      $CC -g -O1 -fsanitize=address -fno-omit-frame-pointer -o main *.c 2>/dev/null || true
    fi
  fi
fi

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake groff libtool pkg-config cmake nasm autopoint po4a libc6:i386 libc6-dev:i386
RUN apt-get install -y make autoconf automake groff libtool pkg-config cmake nasm autopoint po4a libc6:i386 libc6-dev:i386
RUN git clone https://github.com/imagemagick/imagemagick imagemagick
RUN git -C imagemagick checkout 4660af18c654f36f0b054eb3c2d72d1f9928507d
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files: