CVE-2023-1906 - Heap-based Buffer Overflow in ImageMagick
A heap-based buffer overflow was found in ImageMagick after fuzzing. An attacker could cause a denial of service or possibly execute arbitrary code.
Published on blog.agilehunt.com
As part of our security research efforts , we recently used AFL++ (American Fuzzy Lop) to fuzz test ImageMagick, an open-source software suite for creating, editing, and converting images. We compiled ImageMagick with afl-clang-fast and afl-clang-fast++, two powerful compilers built with ASan (Address Sanitizer)…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
ImageMagick is an open-source software suite for creating, editing and converting images. Because it parses a very wide range of image formats, it is a common target for memory-safety research: a malformed file that reaches a decoder can corrupt memory in the process that handles it.
This write-up covers a heap-based buffer overflow found by fuzzing ImageMagick with AFL++. The crash was assigned CVE-2023-1906 and GHSA-35Q2-86C7-9247. According to the report, the flaw could have potentially allowed an attacker to cause a denial of service condition or possibly an execution of arbitrary code.
Setup
The research used AFL++ (American Fuzzy Lop), and ImageMagick was compiled with the afl-clang-fast and afl-clang-fast++ compilers. The compilers were built with ASan (Address Sanitizer), which detects memory errors such as use-after-free and buffer overflows.
The steps below assume AFL++ is installed; the report points to the AFL++ GitHub repository for installation instructions. No specific ImageMagick version is named as the fuzzing target in the report; the fix was released as version 7.1.1-6.
Finding it
The approach was straightforward coverage-guided fuzzing of the compiled ImageMagick binary. The initial corpus came from the AFL demo corpus at https://lcamtuf.coredump.cx/afl/demo/. After running the fuzzer for 24 hours, a crash appeared that ASan attributed to a heap-based buffer overflow.
Exploitation
Step 1: Install the instrumented compilers
Install afl-clang-fast and afl-clang-fast++ following the instructions in the AFL++ GitHub repository.
Step 2: Compile ImageMagick with the AFL++ compilers and ASan
The CC and CXX environment variables select the AFL++ wrappers, and AFL_USE_ASAN=1 turns on Address Sanitizer instrumentation:
AFL_USE_ASAN=1 CC=/usr/local/bin/afl-clang-fast CXX=/usr/local/bin/afl-clang-fast++ ./configure && make -j$(nproc)
./configure && make -j$(nproc) builds ImageMagick with those compilers, parallelised across all available cores. The result is a binary that records coverage for the fuzzer and aborts with an ASan report when it hits a memory error.
Step 3: Generate test cases with afl-fuzz
The fuzzer was run with AFL++'s default settings, seeded with the demo corpus:
afl-fuzz -i /path/to/testcases -o /path/to/output -- /path/to/compiled/imagemagick @@
-i is the input corpus directory, -o the output directory for crashes and queue entries, and @@ is replaced by the path of each generated input file when the target is invoked.
Step 4: Run the PoC crash file
After 24 hours of fuzzing, a crash was found, caused by a heap-based buffer overflow. The crashing input is published as heapoverflow-poc.zip.zip in the ImageMagick GitHub files.

To reproduce, the PoC is passed to the compiled binary as its input file:
/path/to/compiled/imagemagick /path/to/PoC_crash_file
The report gives this command as the way to run the PoC crash file.
Root cause
The report does not describe the faulty code path or the malformed input structure; it only states that the crash is a heap-based buffer overflow detected by ASan. The specific parsing mistake is therefore not documented here. What can be said is that the instrumented build caught an out-of-bounds heap access during processing of a fuzzer-generated file, which is the class of bug ASan is designed to surface immediately rather than let corrupt memory silently.
Impact
Per the report, the vulnerability could have potentially allowed an attacker to cause a denial of service condition or possibly an execution of arbitrary code. The conditions under which a victim would process a malicious file, and the privileges of the processing service, are not discussed in the report.
Fix
The issue was fixed at commit [redacted], and an update was released as version 7.1.1-6. An update was released as version 7.1.1-6.
Variants to test
- Fuzz other image decoders in the same suite with the same ASan-instrumented AFL++ build. A positive result is an ASan heap-buffer-overflow, use-after-free or similar report on a generated input.
- Seed the corpus with samples of less common formats rather than the generic demo corpus. A positive result is new coverage in those format handlers followed by a crash.
- Rebuild with
AFL_USE_ASAN=1and run the published PoC against older builds. A positive result is the ASan report reproducing on the unpatched version and not on 7.1.1-6 or later. - Run the same harness with other sanitizers (for example UBSan) to catch undefined behaviour that ASan alone will not flag. A positive result is a sanitizer diagnostic on an input that does not crash under ASan.
What happened, in plain words
A security researcher let a fuzzer run for 24 hours, feeding ImageMagick automatically generated inputs. a free tool for creating, editing and converting pictures. One of those broken files made ImageMagick crash in a way that pointed to a serious memory bug. The bug was given the identifier CVE-2023-1906 (a public catalogue number for a known vulnerability) and GHSA-35Q2-86C7-9247 (a second catalogue number used by GitHub). According to the report, the flaw could have potentially allowed an attacker to cause a denial of service condition or possibly an execution of arbitrary code.
This article walks through what the researcher did, step by step, in the same order, and explains every term along the way. You only need to know how to use a computer and a browser to follow it.
The product: what ImageMagick is
ImageMagick is an open-source software suite for creating, editing and converting images. "Open source" means the underlying code is published and anyone can read it, use it or improve it. "Converting" means changing a picture from one file format to another, for example turning a PNG into a JPEG.
Because ImageMagick can read a very wide range of image formats, it is a common target for memory-safety research. A "format" here is the internal recipe a file follows, such as JPEG, PNG or GIF. When ImageMagick opens a file, a piece of code called a decoder reads that recipe and turns the bytes into a picture. If the file is malformed, the decoder can be tricked into reading or writing memory it should not touch. That is what "corrupt memory" means: the program's working memory, the scratch space it uses while running, ends up holding the wrong values.
A useful everyday comparison: imagine a librarian who follows a card catalogue to fetch books from shelves. If someone slips a fake card into the catalogue that says "shelf 999, row 999", the librarian may walk into a wall or hand over a book from the wrong place. A malformed image file is that fake card, and the decoder is the librarian.
The vulnerability class: heap-based buffer overflow
A "buffer" is a fixed-size area of memory set aside to hold data, like a row of numbered boxes. A "heap" is the part of a program's memory used for data whose size is decided while the program runs, as opposed to data whose size is fixed when the program is compiled. A "heap-based buffer overflow" happens when a program writes more data into a heap buffer than the buffer can hold, so the extra bytes spill into neighbouring memory.
Why does that matter? Spilled bytes can overwrite values the program relies on, which can make it crash (a denial of service) or, in the worst case, let an attacker influence what the program does next (possibly an execution of arbitrary code). "Arbitrary code" means code the attacker chose, rather than code the program's authors wrote.
The tool: AFL++ and fuzzing
The research used AFL++ (American Fuzzy Lop), a free fuzzer. "Fuzzing" is the practice of feeding a program a huge number of automatically generated, often malformed inputs and watching for crashes. It is like shaking a machine thousands of times in slightly different ways to see which shake makes a bolt fall out, except the shaking is done by software and the inputs are files.
AFL++ is "coverage-guided": it watches which parts of the program's code each input reaches, and it prefers to mutate inputs that reach new code. "Mutate" means make small changes, such as flipping a byte or splicing two files together. This guidance makes fuzzing far more efficient than random guessing.
The build used the afl-clang-fast and afl-clang-fast++ compilers. A "compiler" turns human-written source code into a program the computer can run. These two are special versions of the standard C and C++ compilers that add bookkeeping so the fuzzer can see which code paths an input exercises.
The compilers were built with ASan (Address Sanitizer). ASan is a detector built into the program that watches memory accesses and stops the program the moment it sees an illegal one, such as a use-after-free (using memory after it has been freed) or a buffer overflow. Without ASan, a small overflow might silently corrupt memory and only cause a crash much later, making the bug very hard to trace. With ASan, the program aborts immediately and prints a report pointing at the exact access.
Setup
The steps below assume AFL++ is installed; the report points to the AFL++ GitHub repository for installation instructions. No specific ImageMagick version is named as the fuzzing target in the report; the fix was released as version 7.1.1-6.
Finding it
The approach was straightforward coverage-guided fuzzing of the compiled ImageMagick binary. A "binary" is the finished, runnable program produced by the compiler. The initial corpus came from the AFL demo corpus at https://lcamtuf.coredump.cx/afl/demo/. A "corpus" is the starting set of sample inputs the fuzzer mutates; think of it as the seed packet you give the fuzzer before it starts growing variations. After running the fuzzer for 24 hours, a crash appeared that ASan attributed to a heap-based buffer overflow.
Exploitation
Step 1: Install the instrumented compilers
Install afl-clang-fast and afl-clang-fast++ following the instructions in the AFL++ GitHub repository. "Instrumented" means the compilers have been modified to add the coverage bookkeeping described above. This step comes first because everything else depends on having a build that the fuzzer can watch.
Step 2: Compile ImageMagick with the AFL++ compilers and ASan
The CC and CXX environment variables select the AFL++ wrappers, and AFL_USE_ASAN=1 turns on Address Sanitizer instrumentation:
AFL_USE_ASAN=1 CC=/usr/local/bin/afl-clang-fast CXX=/usr/local/bin/afl-clang-fast++ ./configure && make -j$(nproc)
Line by line, in plain words:
AFL_USE_ASAN=1is an instruction to the AFL++ tooling: switch on Address Sanitizer for this build. It is written as a variable assignment so the tools can read it from the environment, which is the set of named values a program inherits when it starts.CC=/usr/local/bin/afl-clang-fasttells the build system which C compiler to use.CCis the conventional name for that setting, and the path points at the AFL++ C compiler.CXX=/usr/local/bin/afl-clang-fast++does the same for C++, the language ImageMagick is partly written in../configureruns a script that inspects the machine and prepares the build files. The./means "the file called configure in the current folder".&&means "only run the next command if this one succeeded".make -j$(nproc)runs the actual build.-jtells make to run several jobs at once, and$(nproc)is replaced by the number of processor cores the machine has, so the build uses all of them.
The result is a binary that records coverage for the fuzzer and aborts with an ASan report when it hits a memory error. This step is needed because a normal build gives the fuzzer no visibility and no early warning.
Step 3: Generate test cases with afl-fuzz
The fuzzer was run with AFL++'s default settings, seeded with the demo corpus:
afl-fuzz -i /path/to/testcases -o /path/to/output -- /path/to/compiled/imagemagick @@
Line by line:
afl-fuzzis the fuzzer program itself.-i /path/to/testcasesis the input corpus directory, the folder holding the starting samples. The path is a placeholder; you would put your own folder there.-o /path/to/outputis the output directory where the fuzzer writes the inputs it finds interesting, including any that crash.--marks the end of the fuzzer's own options; everything after it is the command to run./path/to/compiled/imagemagickis the compiled program being tested.@@is a placeholder that the fuzzer replaces with the path of each generated input file when it invokes the target. Without it, the program would not know which file to open.
This step is where the actual search happens: the fuzzer mutates the seed files, runs the program on each variation, and keeps the ones that reach new code.
Step 4: Run the PoC crash file
After 24 hours of fuzzing, a crash was found, caused by a heap-based buffer overflow. The crashing input is published as heapoverflow-poc.zip.zip in the ImageMagick GitHub files. "PoC" stands for proof of concept: a small, concrete input that demonstrates the bug.

To reproduce, the PoC is passed to the compiled binary as its input file:
/path/to/compiled/imagemagick /path/to/PoC_crash_file
Line by line:
/path/to/compiled/imagemagickis the ASan-instrumented program built in step 2./path/to/PoC_crash_fileis the crashing input, extracted from the published archive.
Running this command is how you confirm the finding on your own machine. The report gives this command as the way to run the PoC crash file. In other words, the program stops at the moment of the illegal memory access and prints a diagnostic instead of carrying on with corrupted memory.
Root cause
The report does not describe the faulty code path or the malformed input structure; it only states that the crash is a heap-based buffer overflow detected by ASan. The specific parsing mistake is therefore not documented here. What can be said is that the instrumented build caught an out-of-bounds heap access during processing of a fuzzer-generated file, which is the class of bug ASan is designed to surface immediately rather than let corrupt memory silently. "Out-of-bounds" simply means outside the allowed range, like reading past the end of a row of boxes.
Impact
Per the report, the vulnerability could have potentially allowed an attacker to cause a denial of service condition or possibly an execution of arbitrary code. A "denial of service" means making a service unavailable, for example by crashing the program that processes images. The conditions under which a victim would process a malicious file, and the privileges of the processing service, are not discussed in the report. "Privileges" are the permissions a program runs with; a program running with high privileges can do more damage if it is compromised.
Fix
The issue was fixed at commit [redacted], and an update was released as version 7.1.1-6. A "commit" is a recorded change in a code repository, identified by a long string of characters so it can be referred to precisely. An update was released as version 7.1.1-6.
Variants to test
These are follow-up experiments a beginner can try once the basic setup works. Each one describes what to do and what a positive result looks like.
- Fuzz other image decoders in the same suite with the same ASan-instrumented AFL++ build. A positive result is an ASan heap-buffer-overflow, use-after-free or similar report on a generated input. Trying other decoders makes sense because each format has its own parsing code, and a bug found in one does not mean the others are safe.
- Seed the corpus with samples of less common formats rather than the generic demo corpus. A positive result is new coverage in those format handlers followed by a crash. Less common formats are tested less often by other people, so bugs there can survive longer.
- Rebuild with
AFL_USE_ASAN=1and run the published PoC against older builds. A positive result is the ASan report reproducing on the unpatched version and not on 7.1.1-6 or later. This confirms that the fix actually addresses the bug. - Run the same harness with other sanitizers (for example UBSan) to catch undefined behaviour that ASan alone will not flag. A positive result is a sanitizer diagnostic on an input that does not crash under ASan. "Undefined behaviour" is code whose result the language standard does not guarantee, such as signed integer overflow; it may work today and break tomorrow.
Glossary
- Address Sanitizer (ASan): a detector built into a program that stops it the moment it performs an illegal memory access and prints a report.
- AFL++ (American Fuzzy Lop): a free, coverage-guided fuzzer that generates and mutates inputs to find crashes.
- Arbitrary code execution: a situation where an attacker can make a program run code the attacker chose.
- Binary: the finished, runnable program produced by a compiler.
- Buffer: a fixed-size area of memory set aside to hold data.
- Buffer overflow: writing more data into a buffer than it can hold, so the extra bytes spill into neighbouring memory.
- Commit: a recorded change in a code repository, identified by a long string of characters.
- Compiler: a program that turns human-written source code into a runnable program.
- Corpus: the starting set of sample inputs a fuzzer mutates.
- Coverage-guided fuzzing: fuzzing that watches which parts of the code each input reaches and prefers inputs that reach new code.
- CVE: a public catalogue number for a known vulnerability.
- Decoder: the part of a program that reads a file format and turns its bytes into usable data.
- Denial of service: making a service unavailable, for example by crashing the program that processes images.
- Environment variable: a named value a program inherits when it starts.
- Fuzzing: feeding a program a huge number of automatically generated, often malformed inputs and watching for crashes.
- GHSA: a vulnerability catalogue number used by GitHub.
- Heap: the part of a program's memory used for data whose size is decided while the program runs.
- Heap-based buffer overflow: a buffer overflow in which the buffer lives on the heap.
- Instrumented: modified to add bookkeeping, here so the fuzzer can see which code paths an input exercises.
- Malformed file: a file that does not follow the rules of its format.
- Mutate: make a small change to an input, such as flipping a byte or splicing two files together.
- Open source: software whose underlying code is published and can be read, used or improved by anyone.
- Out-of-bounds: outside the allowed range, such as reading past the end of a buffer.
- PoC (proof of concept): a small, concrete input that demonstrates a bug.
- Privileges: the permissions a program runs with.
- Sanitizer: a tool built into a program that detects a class of programming error at runtime.
- Undefined behaviour: code whose result the language standard does not guarantee.
- Use-after-free: using memory after it has been freed.
- UBSan: a sanitizer that detects undefined behaviour.
- Vulnerability: a weakness in software that an attacker could exploit.