1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
From 0a89a1ccca6e7ee059b73f5cc924513383e8a330 Mon Sep 17 00:00:00 2001
From: cristy <cristy@aa41f4f7-0bf4-0310-aa73-e5a19afd5a74>
Date: Sun, 30 Nov 2014 21:54:05 +0000
Subject: [PATCH] Avoid heap overflow in palm, pnm and xpm files
git-svn-id: https://subversion.imagemagick.org/subversion/ImageMagick/branches/ImageMagick-6@17140 aa41f4f7-0bf4-0310-aa73-e5a19afd5a74
origin: http://trac.imagemagick.org/changeset/17140
---
coders/palm.c | 63 ++++++++++++++++++++++++++++-------------------------------
coders/pnm.c | 2 +-
coders/xpm.c | 51 +++++++++++++++++++++++++----------------------
3 files changed, 59 insertions(+), 57 deletions(-)
diff --git a/coders/palm.c b/coders/palm.c
index ccdd353..0e58f91 100644
--- a/coders/palm.c
+++ b/coders/palm.c
@@ -189,7 +189,7 @@ static MagickBooleanType
% o pixel: a pointer to the PixelPacket to be matched.
%
*/
-static int FindColor(PixelPacket *pixel)
+static ssize_t FindColor(PixelPacket *pixel)
{
register ssize_t
i;
@@ -374,26 +374,26 @@ static Image *ReadPALMImage(const ImageInfo *image_info,
for (i=0; i < (ssize_t) count; i++)
{
ReadBlobByte(image);
- index=ConstrainColormapIndex(image,255-i);
- image->colormap[(int) index].red=
- ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
- image->colormap[(int) index].green=
- ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
- image->colormap[(int) index].blue=
- ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
+ index=ConstrainColormapIndex(image,(size_t) (255-i));
+ image->colormap[(int) index].red=ScaleCharToQuantum(
+ (unsigned char) ReadBlobByte(image));
+ image->colormap[(int) index].green=ScaleCharToQuantum(
+ (unsigned char) ReadBlobByte(image));
+ image->colormap[(int) index].blue=ScaleCharToQuantum(
+ (unsigned char) ReadBlobByte(image));
}
}
else
{
for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
{
- index=ConstrainColormapIndex(image,255-i);
- image->colormap[(int) index].red=
- ScaleCharToQuantum(PalmPalette[i][0]);
- image->colormap[(int) index].green=
- ScaleCharToQuantum(PalmPalette[i][1]);
- image->colormap[(int) index].blue=
- ScaleCharToQuantum(PalmPalette[i][2]);
+ index=ConstrainColormapIndex(image,(size_t) (255-i));
+ image->colormap[(int) index].red=ScaleCharToQuantum(
+ PalmPalette[i][0]);
+ image->colormap[(int) index].green=ScaleCharToQuantum(
+ PalmPalette[i][1]);
+ image->colormap[(int) index].blue=ScaleCharToQuantum(
+ PalmPalette[i][2]);
}
}
}
@@ -406,18 +406,18 @@ static Image *ReadPALMImage(const ImageInfo *image_info,
image->storage_class=PseudoClass;
image->depth=8;
}
- one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
- sizeof(*one_row));
+ one_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
+ 2*image->columns),sizeof(*one_row));
if (one_row == (unsigned char *) NULL)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
lastrow=(unsigned char *) NULL;
if (compressionType == PALM_COMPRESSION_SCANLINE) {
- lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
- sizeof(*lastrow));
+ lastrow=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
+ 2*image->columns),sizeof(*lastrow));
if (lastrow == (unsigned char *) NULL)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
}
- mask=(1l << bits_per_pixel)-1;
+ mask=(size_t) (1U << bits_per_pixel)-1;
for (y = 0; y < (ssize_t) image->rows; y++)
{
if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
@@ -453,7 +453,7 @@ static Image *ReadPALMImage(const ImageInfo *image_info,
for (i=0; i < (ssize_t) bytes_per_row; i+=8)
{
count=(ssize_t) ReadBlobByte(image);
- byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
+ byte=(size_t) MagickMin((ssize_t) bytes_per_row-i,8);
for (bit=0; bit < byte; bit++)
{
if ((y == 0) || (count & (one << (7 - bit))))
@@ -478,12 +478,9 @@ static Image *ReadPALMImage(const ImageInfo *image_info,
{
color16=(*ptr++ << 8);
color16|=(*ptr++);
- SetPixelRed(q,(QuantumRange*((color16 >> 11) & 0x1f))/
- 0x1f);
- SetPixelGreen(q,(QuantumRange*((color16 >> 5) & 0x3f))/
- 0x3f);
- SetPixelBlue(q,(QuantumRange*((color16 >> 0) & 0x1f))/
- 0x1f);
+ SetPixelRed(q,(QuantumRange*((color16 >> 11) & 0x1f))/0x1f);
+ SetPixelGreen(q,(QuantumRange*((color16 >> 5) & 0x3f))/0x3f);
+ SetPixelBlue(q,(QuantumRange*((color16 >> 0) & 0x1f))/0x1f);
SetPixelOpacity(q,OpaqueOpacity);
q++;
}
@@ -660,9 +657,6 @@ ModuleExport void UnregisterPALMImage(void)
static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
Image *image)
{
- int
- y;
-
ExceptionInfo
*exception;
@@ -692,6 +686,9 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
register PixelPacket
*p;
+ ssize_t
+ y;
+
size_t
count,
bits_per_pixel,
@@ -848,7 +845,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
sizeof(*one_row));
if (one_row == (unsigned char *) NULL)
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
- for (y=0; y < (int) image->rows; y++)
+ for (y=0; y < (ssize_t) image->rows; y++)
{
ptr=one_row;
(void) ResetMagickMemory(ptr,0,bytes_per_row);
@@ -858,7 +855,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
indexes=GetAuthenticIndexQueue(image);
if (bits_per_pixel == 16)
{
- for (x=0; x < (int) image->columns; x++)
+ for (x=0; x < (ssize_t) image->columns; x++)
{
color16=(unsigned short) ((((31*(size_t) GetPixelRed(p))/
(size_t) QuantumRange) << 11) |
@@ -881,7 +878,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
{
byte=0x00;
bit=(unsigned char) (8-bits_per_pixel);
- for (x=0; x < (int) image->columns; x++)
+ for (x=0; x < (ssize_t) image->columns; x++)
{
if (bits_per_pixel >= 8)
color=(unsigned char) GetPixelIndex(indexes+x);
diff --git a/coders/pnm.c b/coders/pnm.c
index 1d6817e..3fe0dec 100644
--- a/coders/pnm.c
+++ b/coders/pnm.c
@@ -155,7 +155,7 @@ static void PNMComment(Image *image)
Read comment.
*/
comment=AcquireString(GetImageProperty(image,"comment"));
- extent=strlen(comment);
+ extent=MaxTextExtent;
p=comment+strlen(comment);
for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
{
diff --git a/coders/xpm.c b/coders/xpm.c
index 6acd4e9..e48eb9c 100644
--- a/coders/xpm.c
+++ b/coders/xpm.c
@@ -152,12 +152,16 @@ static int CompareXPMColor(const void *target,const void *source)
return(strcmp(p,q));
}
-static char *CopyXPMColor(char *destination,const char *source,size_t length)
+static size_t CopyXPMColor(char *destination,const char *source,size_t length)
{
- while (length-- && (*source != '\0'))
- *destination++=(*source++);
+ register char
+ *p;
+
+ p=source;
+ while (length-- && (*p != '\0'))
+ *destination++=(*p++);
*destination='\0';
- return(destination-length);
+ return((size_t) (p-source));
}
static char *NextXPMLine(char *p)
@@ -307,24 +311,26 @@ static Image *ReadXPMImage(const ImageInfo *image_info,ExceptionInfo *exception)
*/
length=MaxTextExtent;
xpm_buffer=(char *) AcquireQuantumMemory((size_t) length,sizeof(*xpm_buffer));
+ if (xpm_buffer == (char *) NULL)
+ ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+ *xpm_buffer='\0';
p=xpm_buffer;
- if (xpm_buffer != (char *) NULL)
- while (ReadBlobString(image,p) != (char *) NULL)
- {
- if ((*p == '#') && ((p == xpm_buffer) || (*(p-1) == '\n')))
- continue;
- if ((*p == '}') && (*(p+1) == ';'))
- break;
- p+=strlen(p);
- if ((size_t) (p-xpm_buffer+MaxTextExtent) < length)
- continue;
- length<<=1;
- xpm_buffer=(char *) ResizeQuantumMemory(xpm_buffer,length+MaxTextExtent,
- sizeof(*xpm_buffer));
- if (xpm_buffer == (char *) NULL)
- break;
- p=xpm_buffer+strlen(xpm_buffer);
- }
+ while (ReadBlobString(image,p) != (char *) NULL)
+ {
+ if ((*p == '#') && ((p == xpm_buffer) || (*(p-1) == '\n')))
+ continue;
+ if ((*p == '}') && (*(p+1) == ';'))
+ break;
+ p+=strlen(p);
+ if ((size_t) (p-xpm_buffer+MaxTextExtent) < length)
+ continue;
+ length<<=1;
+ xpm_buffer=(char *) ResizeQuantumMemory(xpm_buffer,length+MaxTextExtent,
+ sizeof(*xpm_buffer));
+ if (xpm_buffer == (char *) NULL)
+ break;
+ p=xpm_buffer+strlen(xpm_buffer);
+ }
if (xpm_buffer == (char *) NULL)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
/*
@@ -438,13 +444,12 @@ static Image *ReadXPMImage(const ImageInfo *image_info,ExceptionInfo *exception)
indexes=GetAuthenticIndexQueue(image);
for (x=0; x < (ssize_t) image->columns; x++)
{
- (void) CopyXPMColor(key,p,(size_t) width);
+ p+=CopyXPMColor(key,p,MagickMin(width,MaxTextExtent));
j=(ssize_t) GetValueFromSplayTree(xpm_colors,key);
if (image->storage_class == PseudoClass)
SetPixelIndex(indexes+x,j);
*r=image->colormap[j];
r++;
- p+=width;
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
--
2.1.4
|