Commit b9dda66ec for imagemagick.org
commit b9dda66ec142701b40c78fbc33505043617a3674
Author: Cristy <urban-warrior@imagemagick.org>
Date: Mon Nov 23 19:29:16 2026 -0500
an XMP segment with no NUL terminator frees the buffer
diff --git a/MagickCore/deprecate.c b/MagickCore/deprecate.c
index 000115a48..496165b5e 100644
--- a/MagickCore/deprecate.c
+++ b/MagickCore/deprecate.c
@@ -195,6 +195,9 @@ MagickExport void *CropImageToHBITMAP(Image *image,
RGBQUAD
*bitmap_bits;
+ size_t
+ length;
+
ssize_t
y;
@@ -228,29 +231,45 @@ MagickExport void *CropImageToHBITMAP(Image *image,
page.height+=page.y;
page.y=0;
}
-
if ((page.width == 0) || (page.height == 0))
ThrowImageException(OptionError,"GeometryDimensionsAreZero");
+ /*
+ The BITMAP structure uses LONG dimensions; make sure the pixel buffer size
+ cannot overflow.
+ */
+ if ((page.width > (size_t) (INT_MAX/sizeof(RGBQUAD))) ||
+ (page.height > ((size_t) INT_MAX/(page.width*sizeof(RGBQUAD)))))
+ ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
+ if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
+ if (SetImageColorspace(image,sRGBColorspace,exception) == MagickFalse)
+ return((void *) NULL);
/*
Initialize crop image attributes.
*/
- bitmap.bmType = 0;
- bitmap.bmWidth = (LONG) page.width;
- bitmap.bmHeight = (LONG) page.height;
- bitmap.bmWidthBytes = bitmap.bmWidth * 4;
- bitmap.bmPlanes = 1;
- bitmap.bmBitsPixel = 32;
- bitmap.bmBits = NULL;
-
- bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,page.width*
- page.height*bitmap.bmBitsPixel);
+ (void) memset(&bitmap,0,sizeof(bitmap));
+ bitmap.bmType=0;
+ bitmap.bmWidth=(LONG) page.width;
+ bitmap.bmHeight=(LONG) page.height;
+ bitmap.bmWidthBytes=(LONG) (page.width*sizeof(RGBQUAD));
+ bitmap.bmPlanes=1;
+ bitmap.bmBitsPixel=32;
+ bitmap.bmBits=NULL;
+ length=page.width*page.height*sizeof(RGBQUAD); /* bytes, not bits */
+ bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
if (bitmap_bitsH == NULL)
- return(NULL);
+ {
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+ return((void *) NULL);
+ }
bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
- if ( bitmap.bmBits == NULL )
- bitmap.bmBits = bitmap_bits;
- if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
- SetImageColorspace(image,sRGBColorspace,exception);
+ if (bitmap_bits == (RGBQUAD *) NULL)
+ {
+ GlobalFree((HGLOBAL) bitmap_bitsH);
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+ return((void *) NULL);
+ }
/*
Extract crop image.
*/
@@ -263,14 +282,15 @@ MagickExport void *CropImageToHBITMAP(Image *image,
p=GetVirtualPixels(image,page.x,page.y+y,page.width,1,exception);
if (p == (const Quantum *) NULL)
break;
-
- /* Transfer pixels, scaling to Quantum */
- for( x=(ssize_t) page.width ; x> 0 ; x-- )
+ /*
+ Transfer pixels, scaling to Quantum.
+ */
+ for (x=0; x < (ssize_t) page.width; x++)
{
- q->rgbRed = ScaleQuantumToChar(GetPixelRed(image,p));
- q->rgbGreen = ScaleQuantumToChar(GetPixelGreen(image,p));
- q->rgbBlue = ScaleQuantumToChar(GetPixelBlue(image,p));
- q->rgbReserved = 0;
+ q->rgbRed=ScaleQuantumToChar(GetPixelRed(image,p));
+ q->rgbGreen=ScaleQuantumToChar(GetPixelGreen(image,p));
+ q->rgbBlue=ScaleQuantumToChar(GetPixelBlue(image,p));
+ q->rgbReserved=0;
p+=(ptrdiff_t) GetPixelChannels(image);
q++;
}
@@ -288,6 +308,9 @@ MagickExport void *CropImageToHBITMAP(Image *image,
bitmapH=CreateBitmapIndirect(&bitmap);
GlobalUnlock((HGLOBAL) bitmap_bitsH);
GlobalFree((HGLOBAL) bitmap_bitsH);
+ if (bitmapH == (HBITMAP) NULL)
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
return((void *) bitmapH);
}
#endif
@@ -308,12 +331,14 @@ MagickExport void *CropImageToHBITMAP(Image *image,
%
% The format of the ImageToHBITMAP method is:
%
-% HBITMAP ImageToHBITMAP(Image *image,Exceptioninfo *exception)
+% HBITMAP ImageToHBITMAP(Image *image,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: the image to convert.
%
+% o exception: return any errors or warnings in this structure.
+%
*/
MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
{
@@ -344,32 +369,51 @@ MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
ssize_t
y;
+ assert(image != (Image *) NULL);
+ assert(image->signature == MagickCoreSignature);
+ if (IsEventLogging() != MagickFalse)
+ (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+ assert(exception != (ExceptionInfo *) NULL);
+ assert(exception->signature == MagickCoreSignature);
+ /*
+ The BITMAP structure uses LONG dimensions; make sure the pixel buffer size
+ cannot overflow.
+ */
+ if ((image->columns == 0) || (image->rows == 0) ||
+ (image->columns > (size_t) (INT_MAX/sizeof(RGBQUAD))) ||
+ (image->rows > ((size_t) INT_MAX/(image->columns*sizeof(RGBQUAD)))))
+ {
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+ return((void *) NULL);
+ }
+ if (SetImageColorspace(image,sRGBColorspace,exception) == MagickFalse)
+ return((void *) NULL);
(void) memset(&bitmap,0,sizeof(bitmap));
bitmap.bmType=0;
bitmap.bmWidth=(LONG) image->columns;
bitmap.bmHeight=(LONG) image->rows;
- bitmap.bmWidthBytes=4*bitmap.bmWidth;
+ bitmap.bmWidthBytes=(LONG) (image->columns*sizeof(RGBQUAD));
bitmap.bmPlanes=1;
bitmap.bmBitsPixel=32;
bitmap.bmBits=NULL;
- length=bitmap.bmWidthBytes*bitmap.bmHeight;
+ length=image->columns*image->rows*sizeof(RGBQUAD);
bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
if (bitmap_bitsH == NULL)
{
- char
- *message;
-
- message=GetExceptionMessage(errno);
(void) ThrowMagickException(exception,GetMagickModule(),
- ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
- message=DestroyString(message);
- return(NULL);
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+ return((void *) NULL);
}
bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
+ if (bitmap_bits == (RGBQUAD *) NULL)
+ {
+ GlobalFree((HGLOBAL) bitmap_bitsH);
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+ return((void *) NULL);
+ }
q=bitmap_bits;
- if (bitmap.bmBits == NULL)
- bitmap.bmBits=bitmap_bits;
- (void) SetImageColorspace(image,sRGBColorspace,exception);
for (y=0; y < (ssize_t) image->rows; y++)
{
p=GetVirtualPixels(image,0,y,image->columns,1,exception);
@@ -385,20 +429,22 @@ MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
q++;
}
}
- bitmap.bmBits=bitmap_bits;
- bitmapH=CreateBitmapIndirect(&bitmap);
- if (bitmapH == NULL)
+ if (y < (ssize_t) image->rows)
{
- char
- *message;
-
- message=GetExceptionMessage(errno);
- (void) ThrowMagickException(exception,GetMagickModule(),
- ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
- message=DestroyString(message);
+ /*
+ Pixel read failed; do not return a partially filled bitmap.
+ */
+ GlobalUnlock((HGLOBAL) bitmap_bitsH);
+ GlobalFree((HGLOBAL) bitmap_bitsH);
+ return((void *) NULL);
}
+ bitmap.bmBits=bitmap_bits;
+ bitmapH=CreateBitmapIndirect(&bitmap);
GlobalUnlock((HGLOBAL) bitmap_bitsH);
GlobalFree((HGLOBAL) bitmap_bitsH);
+ if (bitmapH == (HBITMAP) NULL)
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
return((void *) bitmapH);
}
#endif
@@ -428,8 +474,11 @@ MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
MagickExport void InitializePixelChannelMap(Image *image)
{
ExceptionInfo
- *exception = AcquireExceptionInfo();
+ *exception;
+ assert(image != (Image *) NULL);
+ assert(image->signature == MagickCoreSignature);
+ exception=AcquireExceptionInfo();
(void) ResetPixelChannelMap(image,exception);
exception=DestroyExceptionInfo(exception);
}
diff --git a/coders/jpeg.c b/coders/jpeg.c
index f9b8dade2..0840e3841 100644
--- a/coders/jpeg.c
+++ b/coders/jpeg.c
@@ -112,6 +112,7 @@
#define IPTC_INDEX 13
#define IPTC_MARKER (JPEG_APP0+IPTC_INDEX)
#define MaxJPEGProfiles 16
+#define MPF_INDEX MaxJPEGProfiles /* spare slot: keeps MPF data out of ICC */
#define MaxJPEGScans 1024
/*
@@ -957,13 +958,13 @@ static boolean ReadICCProfile(j_decompress_ptr jpeg_info)
/*
Multi-picture support (Future).
*/
- status=ReadProfilePayload(jpeg_info,ICC_INDEX,length-prefix_length);
+ status=ReadProfilePayload(jpeg_info,MPF_INDEX,length-prefix_length);
if (status == MagickFalse)
return(FALSE);
status=SetImageProfile(client_info->image,"MPF",
- client_info->profiles[ICC_INDEX],client_info->exception);
- client_info->profiles[ICC_INDEX]=DestroyStringInfo(
- client_info->profiles[ICC_INDEX]);
+ client_info->profiles[MPF_INDEX],client_info->exception);
+ client_info->profiles[MPF_INDEX]=DestroyStringInfo(
+ client_info->profiles[MPF_INDEX]);
return(TRUE);
}
gainmap_signature=GetGainmapSignatureWithPrefix((const unsigned char *)
@@ -1076,7 +1077,7 @@ static boolean ReadIPTCProfile(j_decompress_ptr jpeg_info)
magick[10]='\0';
length-=10;
if (length <= 10)
- return(TRUE);
+ return(SkipProfilePayload(jpeg_info,length) != MagickFalse ? TRUE : FALSE);
if (LocaleCompare(magick,"Photoshop ") != 0)
{
/*
@@ -1091,7 +1092,7 @@ static boolean ReadIPTCProfile(j_decompress_ptr jpeg_info)
Remove the version number.
*/
if (length <= 15)
- return(TRUE);
+ return(SkipProfilePayload(jpeg_info,length) != MagickFalse ? TRUE : FALSE);
for (i=0; i < 4; i++)
if (GetCharacter(jpeg_info) == EOF)
break;
@@ -1189,9 +1190,10 @@ static boolean ReadAPPProfiles(j_decompress_ptr jpeg_info)
SetStringInfoLength(profile,length-i-1);
status=SetImageProfilePrivate(image,profile,exception);
}
- client_info->profiles[marker]=DestroyStringInfo(
- client_info->profiles[marker]);
}
+ if (client_info->profiles[marker] != (StringInfo *) NULL)
+ client_info->profiles[marker]=DestroyStringInfo(
+ client_info->profiles[marker]);
}
else
{
@@ -1461,7 +1463,7 @@ static JPEGClientInfo *JPEGCleanup(struct jpeg_decompress_struct *jpeg_info,
if (client_info != (JPEGClientInfo *) NULL)
{
- for (i=0; i < MaxJPEGProfiles; i++)
+ for (i=0; i <= MaxJPEGProfiles; i++)
{
if (client_info->profiles[i] != (StringInfo *) NULL)
client_info->profiles[i]=DestroyStringInfo(client_info->profiles[i]);
@@ -1472,20 +1474,35 @@ static JPEGClientInfo *JPEGCleanup(struct jpeg_decompress_struct *jpeg_info,
return(client_info);
}
-static void JPEGDestroyImageProfiles(JPEGClientInfo *client_info)
+static void JPEGSetImageProfiles(JPEGClientInfo *client_info,Image *image,
+ ExceptionInfo *exception)
{
+ char
+ name[MagickPathExtent];
+
ssize_t
i;
StringInfo
*profile;
- for (i=0; i < MaxJPEGProfiles; i++)
+ /*
+ Attach the generic APPn segments that no dedicated handler claimed (APP1,
+ APP2 and APP13 are handled elsewhere, COM is a property, APP14 is
+ ignored), then release every temporary profile buffer.
+ */
+ for (i=0; i <= MaxJPEGProfiles; i++)
{
profile=client_info->profiles[i];
if (profile == (StringInfo *) NULL)
continue;
- client_info->profiles[i]=DestroyStringInfo(client_info->profiles[i]);
+ if ((i > ICC_INDEX) && (i < MaxJPEGProfiles) && (i != IPTC_INDEX) &&
+ (i != 14))
+ {
+ (void) FormatLocaleString(name,MagickPathExtent,"APP%d",(int) i);
+ (void) SetImageProfile(image,name,profile,exception);
+ }
+ client_info->profiles[i]=DestroyStringInfo(profile);
}
}
@@ -1617,7 +1634,14 @@ static Image *ReadOneJPEGImage(const ImageInfo *image_info,
ReadAPPProfiles);
}
}
- (void) jpeg_read_header(jpeg_info,TRUE);
+ if (jpeg_read_header(jpeg_info,TRUE) != JPEG_HEADER_OK)
+ {
+ /*
+ A marker handler suspended the decoder: the header is incomplete.
+ */
+ client_info=JPEGCleanup(jpeg_info,client_info);
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ }
#if defined(MAGICKCORE_UHDR_DELEGATE)
if ((client_info->uhdr_metadata != MagickFalse) && (*offset == 0) &&
(GetBlobStreamData(image) != (const unsigned char *) NULL) &&
@@ -1749,7 +1773,8 @@ static Image *ReadOneJPEGImage(const ImageInfo *image_info,
jpeg_info->scale_denom=8U;
#else
jpeg_info->scale_num=1U;
- jpeg_info->scale_denom=(unsigned int) scale_factor;
+ jpeg_info->scale_denom=(scale_factor < 1.0) ? 1U :
+ (unsigned int) scale_factor; /* a zero denominator divides by zero */
#endif
jpeg_calc_output_dimensions(jpeg_info);
if (image->debug != MagickFalse)
@@ -1901,7 +1926,7 @@ static Image *ReadOneJPEGImage(const ImageInfo *image_info,
if (jpeg_info->arith_code == TRUE)
(void) SetImageProperty(image,"jpeg:arithmetic-coding","true",exception);
#endif
- JPEGDestroyImageProfiles(client_info);
+ JPEGSetImageProfiles(client_info,image,exception);
*offset=TellBlob(image);
if (image_info->ping != MagickFalse)
{
@@ -2014,7 +2039,10 @@ static Image *ReadOneJPEGImage(const ImageInfo *image_info,
*/
q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
if (q == (Quantum *) NULL)
- break;
+ {
+ status=MagickFalse;
+ break;
+ }
p=jpeg_pixels;
switch (jpeg_info->output_components)
{
@@ -2084,7 +2112,10 @@ static Image *ReadOneJPEGImage(const ImageInfo *image_info,
}
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
- break;
+ {
+ status=MagickFalse;
+ break;
+ }
status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
@@ -2167,7 +2198,11 @@ static MagickBooleanType ReadMPOImages(const ImageInfo *image_info,
if ((buffer[i] != signature[j]) && (buffer[i] != alt_signature[j]))
{
- j=0;
+ /*
+ Restart the match, but let this byte begin a new signature (an
+ 0xff run such as ff ff d8 ff e0 would otherwise be missed).
+ */
+ j=(buffer[i] == signature[0]) ? 1 : 0;
continue;
}
if (++j != SIGNATURE_SIZE)
@@ -2391,7 +2426,7 @@ ModuleExport size_t RegisterJPEGImage(void)
*/
ModuleExport void UnregisterJPEGImage(void)
{
- (void) UnregisterMagickInfo("PJPG");
+ (void) UnregisterMagickInfo("PJPEG");
(void) UnregisterMagickInfo("MPO");
(void) UnregisterMagickInfo("JPS");
(void) UnregisterMagickInfo("JPG");
@@ -2722,10 +2757,17 @@ static void WriteProfiles(j_compress_ptr jpeg_info,Image *image,
int
marker;
- marker=APP_MARKER+StringToInteger(name+3);
- for (i=0; i < (ssize_t) length; i+=65533L)
- jpeg_write_marker(jpeg_info,marker,GetStringInfoDatum(profile)+i,
- MagickMin((unsigned int) (length-i),65533));
+ marker=StringToInteger(name+3);
+ if ((marker < 0) || (marker > 15))
+ (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
+ "InvalidAPPMarker","`%s'",image->filename);
+ else
+ {
+ marker+=APP_MARKER;
+ for (i=0; i < (ssize_t) length; i+=65533L)
+ jpeg_write_marker(jpeg_info,marker,GetStringInfoDatum(profile)+i,
+ MagickMin((unsigned int) (length-i),65533));
+ }
}
else if (LocaleCompare(name,"EXIF") == 0)
{
@@ -2750,7 +2792,7 @@ static void WriteProfiles(j_compress_ptr jpeg_info,Image *image,
length=(size_t) MagickMin((ssize_t) GetStringInfoLength(profile)-i,
65519L);
p[12]=(unsigned char) ((i/65519L)+1);
- p[13]=(unsigned char) (GetStringInfoLength(profile)/65519L+1);
+ p[13]=(unsigned char) ((GetStringInfoLength(profile)+65518L)/65519L);
(void) memcpy(p+tag_length+3,GetStringInfoDatum(profile)+i,
length);
jpeg_write_marker(jpeg_info,ICC_MARKER,GetStringInfoDatum(
@@ -2806,9 +2848,19 @@ static void WriteProfiles(j_compress_ptr jpeg_info,Image *image,
ConcatenateStringInfo(xmp_profile,profile);
GetStringInfoDatum(xmp_profile)[strlen(xmp_namespace)]='\0';
length=GetStringInfoLength(xmp_profile);
- for (i=0; i < (ssize_t) length; i+=65533L)
+ if (length > 65533L)
+ {
+ /*
+ Continuation segments would lack the namespace header and
+ produce a corrupt stream (Extended XMP is not supported).
+ */
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ CoderWarning,"XmpProfileSizeExceedsLimit","`%s'",
+ image->filename);
+ }
+ else
jpeg_write_marker(jpeg_info,APP_MARKER+1,GetStringInfoDatum(
- xmp_profile)+i,MagickMin((unsigned int) (length-i),65533));
+ xmp_profile),(unsigned int) length);
xmp_profile=DestroyStringInfo(xmp_profile);
}
if (image->debug != MagickFalse)
@@ -3004,7 +3056,16 @@ static MagickBooleanType WriteJPEGImage_(const ImageInfo *image_info,
JPEGDestinationManager(jpeg_info,image);
if ((image->columns != (unsigned int) image->columns) ||
(image->rows != (unsigned int) image->rows))
- ThrowJPEGWriterException(ImageError,"WidthOrHeightExceedsLimit");
+ {
+ jpeg_destroy_compress(jpeg_info);
+ client_info=(JPEGClientInfo *) RelinquishMagickMemory(client_info);
+ (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
+ "WidthOrHeightExceedsLimit","`%s'",image->filename);
+ (void) CloseBlob(image);
+ if (jps_image != (Image *) NULL)
+ jps_image=DestroyImage(jps_image);
+ return(MagickFalse);
+ }
jpeg_info->image_width=(unsigned int) image->columns;
jpeg_info->image_height=(unsigned int) image->rows;
jpeg_info->input_components=3;
@@ -3337,7 +3398,6 @@ static MagickBooleanType WriteJPEGImage_(const ImageInfo *image_info,
" Input sampling-factors=%s",sampling_factor);
}
}
- value=GetImageOption(image_info,"jpeg:sampling-factor");
if (image_info->sampling_factor != (char *) NULL)
sampling_factor=image_info->sampling_factor;
if (sampling_factor == (const char *) NULL)
@@ -3532,7 +3592,16 @@ static MagickBooleanType WriteJPEGImage_(const ImageInfo *image_info,
memory_info=AcquireVirtualMemory((size_t) image->columns,
(size_t) (jpeg_info->input_components*bytes_per_pixel));
if (memory_info == (MemoryInfo *) NULL)
- ThrowJPEGWriterException(ResourceLimitError,"MemoryAllocationFailed");
+ {
+ jpeg_destroy_compress(jpeg_info);
+ client_info=(JPEGClientInfo *) RelinquishMagickMemory(client_info);
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+ (void) CloseBlob(image);
+ if (jps_image != (Image *) NULL)
+ jps_image=DestroyImage(jps_image);
+ return(MagickFalse);
+ }
jpeg_pixels=(JSAMPLE *) GetVirtualMemoryBlob(memory_info);
if (setjmp(client_info->error_recovery) != 0)
{
@@ -3546,6 +3615,7 @@ static MagickBooleanType WriteJPEGImage_(const ImageInfo *image_info,
return(MagickFalse);
}
range=GetQuantumRange(jpeg_info->data_precision);
+ status=MagickTrue; /* earlier steps (extent search, resources) reuse status */
for (y=0; y < (ssize_t) image->rows; y++)
{
const Quantum