We got a message from GitHub asking us to have our project scanned with MDASH. We received this invite because we were part of the GitHub Secure Open Source Fund, which aims to improve the security of open source projects. I was a bit sceptical at first. Most of the security advisories that we receive are written by or with the help of AI. But the quality is not always great, and they almost never include a ready-to-use fix. After reading the MDASH documentation, I could see that it would provide both a detailed explanation of the issue and a code fix. Because of this, I decided to take advantage of the opportunity and have our project scanned.
GitHub and the Microsoft FORGE Lab set up the scan for our project. We received a summary of the findings from this scan. Because we receive so many valid and invalid security reports, we have set a limit on the number of security reports that we accept per day. People often think that we have unlimited capacity to handle reports, but in reality, we have only two developers handling them. To make it possible to receive all the reports from the MDASH scan, we had to add the MDASH team as a trusted reporter so they would not be subject to our daily limit. But we also did not want to receive all of them in one go, so we asked them to send us the high-severity reports first. I will not discuss all of the findings here, but I will go through one of them in detail.
One of the security reports that the ImageMagick project received from MDASH had the following title:
coders/exr: unchecked 32-bit row stride in InitializeEXRChannels causes heap out-of-bounds write
We have had reports about things that only happen in 32-bit mode, and this looked like another one of those. But after reading the summary from MDASH, it became clear that this issue could also affect 64-bit builds.
When decoding an EXR image through OpenEXRCore,
InitializeEXRChannels()allocates the decoded pixel buffer with 64-bitsize_tarithmetic, but narrows the per-row byte stridecolumns * (*pixel_size)into the signed 32-bit fieldchannel->user_line_stridewithout checkingINT32_MAX. A crafted EXR image with a wide row and multiple channels makesuser_line_stridewrap to a large negative value, causing OpenEXRCore to write row 1 roughly 2 GiB before the start of the decode buffer into unrelated heap memory.
As you can see, this is a very clear and detailed explanation of the issue, showing exactly how a crafted EXR image can trigger a heap out-of-bounds write in 64-bit builds. It is short and to the point, making it easy to understand the root cause of the problem.
The next part of their report is also something we don’t see often in reports. It showed the exact parts of our code where this happens:
/* coders/exr.c: InitializeEXRChannels() */
286: *data=(uint8_t *) AcquireQuantumMemory(*pixel_size,pixel_count); /* allocates 4,294,967,360 bytes (64-bit size_t) */
...
293: for (c=0; c < decoder.channel_count; c++)
294: {
295: channel->decode_to_ptr=p;
296: channel->user_pixel_stride=(int32_t) *pixel_size;
297: channel->user_line_stride=(int32_t) (columns*(*pixel_size)); /* BUG: 0x80000020 wraps to -2,147,483,616 */
298: p+=(ptrdiff_t) channel->bytes_per_element;
299: channel++;
300: }
/* coders/exr.c: ReadEXRTiledImage() */
536: result=exr_decoding_run(ctxt,part_index,&decoder); /* row 1 written ~2 GiB before *data */
This clearly shows that the issue is triggered by the calculation of the per-row byte stride and the subsequent write into the buffer, which can go out of bounds when the stride wraps around. We do get code snippets or individual lines in other reports, but this style of showing the exact lines and context in the source code is much more helpful for understanding and fixing the issue.
Most security reports we receive include reproduction instructions—not just those from MDASH. I usually start by reading the explanation and the AddressSanitizer output to understand the issue. Sometimes I also follow the instructions to create the input file myself and reproduce the issue. In this case, the explanation and the AddressSanitizer output were enough to understand the problem.
==583521==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7a955a03d890 at pc 0x7a966d095e44 bp 0x7ffecbee0b90 sp 0x7ffecbee0b80
WRITE of size 4 at 0x7a955a03d890 thread T0
#0 0x7a966d095e43 in unpack_32bit /tmp/im-medium-test/openexr/src/lib/OpenEXRCore/unpack.c:961
#1 0x7a966d08f660 in exr_decoding_run /tmp/im-medium-test/openexr/src/lib/OpenEXRCore/decoding.c:711
#2 0x62d5dfcaa2bf in ReadEXRTiledImage ../coders/exr.c:536
#3 0x62d5dfcaa2bf in ReadEXRImage ../coders/exr.c:678
0x7a955a03d890 is located 80 bytes after 4294967360-byte region [0x7a945a03d800,0x7a955a03d840)
Together with the explanation, this output made it immediately clear that this was a security bug that needed a fix.
For most security advisories, we would need to write the fix ourselves or with the help of GitHub Copilot. But this report finally included a patch that we could use directly to fix the issue.
diff --git a/coders/exr.c b/coders/exr.c
--- a/coders/exr.c
+++ b/coders/exr.c
@@ -283,6 +283,10 @@ static MagickBooleanType InitializeEXRChannels(Image *image,
return(status);
if ((decoder.channel_count == 1) && (pixel_channels[0] == IndexPixelChannel))
image->colorspace=GRAYColorspace;
+ if ((*pixel_size == 0) ||
+ (columns > ((size_t) INT32_MAX/(*pixel_size))))
+ ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+ image->filename);
*data=(uint8_t *) AcquireQuantumMemory(*pixel_size,pixel_count);
if (*data == (uint8_t*)NULL)
ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
We applied this patch with a tiny change in code style and pushed the fix. Normally, a security advisory would take us around 30 minutes to an hour to read, understand, and write a fix for. In this case, it probably took around 15 minutes. That might not sound like a big difference, but if you take into account the number of valid and invalid reports that we get (512 this year), it makes a huge difference.
Because the rest of the initial batch of reports from MDASH were of the same high quality, we were able to address them efficiently as well. We then asked the team to send us the rest of the reports through GitHub Private Vulnerability Reporting. Because we wanted to give the reports a proper review, we took our time to carefully review and address each one. All 22 reported issues are now fixed, and the fixes are included in our latest release.
You can read more about this and the other projects that participated here: https://github.blog/open-source/maintainers/maintainers-dont-need-more-ai-bug-reports-they-need-fixes/.
</@dlemstra>