Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Source/FreeImage/PluginPICT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,13 @@ UnpackBits( FreeImageIO *io, fi_handle handle, FIBITMAP* dib, MacRect* bounds, W
}
}
else {
for ( int i = 0; i < height; i++ ) {
// CWE-787: bound every PackBits write to the allocated raster.
// The RLE run length is attacker-controlled and the loop below
// advances dst without checking it against the bitmap bounds.
BYTE* const rasterStart = FreeImage_GetBits( dib );
BYTE* const rasterEnd = rasterStart +
(size_t)FreeImage_GetPitch( dib ) * FreeImage_GetHeight( dib );
for ( int i = 0; i < height; i++ ) {
// For each line do...
int linelen; // length of source line in bytes.
if (rowBytes > 250) {
Expand Down Expand Up @@ -784,16 +790,22 @@ UnpackBits( FreeImageIO *io, fi_handle handle, FIBITMAP* dib, MacRect* bounds, W

// This is slow for some formats...
if (pixelSize == 16) {
if (dst < rasterStart ||
(size_t)(rasterEnd - dst) < (size_t)len*4*PixelPerRLEUnit)
break;
expandBuf( io, handle, 1, pixelSize, dst );
for ( int k = 1; k < len; k++ ) {
for ( int k = 1; k < len; k++ ) {
// Repeat the pixel len times.
memcpy( dst+(k*4*PixelPerRLEUnit), dst, 4*PixelPerRLEUnit);
}
dst += len*4*PixelPerRLEUnit;
}
else {
if (dst < rasterStart ||
(size_t)(rasterEnd - dst) < (size_t)len*PixelPerRLEUnit)
break;
expandBuf8( io, handle, 1, pixelSize, dst );
for ( int k = 1; k < len; k++ ) {
for ( int k = 1; k < len; k++ ) {
// Repeat the expanded byte len times.
memcpy( dst+(k*PixelPerRLEUnit), dst, PixelPerRLEUnit);
}
Expand All @@ -806,10 +818,16 @@ UnpackBits( FreeImageIO *io, fi_handle handle, FIBITMAP* dib, MacRect* bounds, W
// Unpacked data
int len = (FlagCounter & 255) + 1;
if (pixelSize == 16) {
if (dst < rasterStart ||
(size_t)(rasterEnd - dst) < (size_t)len*4*PixelPerRLEUnit)
break;
expandBuf( io, handle, len, pixelSize, dst );
dst += len*4*PixelPerRLEUnit;
}
else {
if (dst < rasterStart ||
(size_t)(rasterEnd - dst) < (size_t)len*PixelPerRLEUnit)
break;
expandBuf8( io, handle, len, pixelSize, dst );
dst += len*PixelPerRLEUnit;
}
Expand Down