50a37d
From cc9658731ba1ea291f83ea959acc27ac28384a2c Mon Sep 17 00:00:00 2001
50a37d
From: Lumir Balhar <lbalhar@redhat.com>
50a37d
Date: Fri, 14 Feb 2020 11:14:53 +0100
50a37d
Subject: [PATCH] Combined fix for CVE-2020-5312 and CVE-2019-16865
50a37d
50a37d
---
50a37d
 src/PIL/GifImagePlugin.py     |  1 +
50a37d
 src/PIL/IcoImagePlugin.py     |  1 +
50a37d
 src/PIL/PsdImagePlugin.py     |  6 ++++--
50a37d
 src/PIL/TiffImagePlugin.py    |  4 ++--
50a37d
 src/libImaging/FliDecode.c    | 14 +++++++++++---
50a37d
 src/libImaging/PcxDecode.c    |  8 ++++++++
50a37d
 src/libImaging/RawDecode.c    | 11 +++++++++--
50a37d
 src/libImaging/SgiRleDecode.c |  5 +++++
50a37d
 8 files changed, 41 insertions(+), 9 deletions(-)
50a37d
50a37d
diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
50a37d
index c01adff..99af4a5 100644
50a37d
--- a/src/PIL/GifImagePlugin.py
50a37d
+++ b/src/PIL/GifImagePlugin.py
50a37d
@@ -251,6 +251,7 @@ class GifImageFile(ImageFile.ImageFile):
50a37d
                 self.dispose = None
50a37d
             elif self.disposal_method == 2:
50a37d
                 # replace with background colour
50a37d
+                Image._decompression_bomb_check(self.size)
50a37d
                 self.dispose = Image.core.fill("P", self.size,
50a37d
                                                self.info["background"])
50a37d
             else:
50a37d
diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py
50a37d
index 428fdd4..2b6d1e0 100644
50a37d
--- a/src/PIL/IcoImagePlugin.py
50a37d
+++ b/src/PIL/IcoImagePlugin.py
50a37d
@@ -167,6 +167,7 @@ class IcoFile(object):
50a37d
         else:
50a37d
             # XOR + AND mask bmp frame
50a37d
             im = BmpImagePlugin.DibImageFile(self.buf)
50a37d
+            Image._decompression_bomb_check(im.size)
50a37d
 
50a37d
             # change tile dimension to only encompass XOR image
50a37d
             im.size = (im.size[0], int(im.size[1] / 2))
50a37d
diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py
50a37d
index f6e04f7..fe2a2ff 100644
50a37d
--- a/src/PIL/PsdImagePlugin.py
50a37d
+++ b/src/PIL/PsdImagePlugin.py
50a37d
@@ -209,9 +209,11 @@ def _layerinfo(file):
50a37d
         # skip over blend flags and extra information
50a37d
         filler = read(12)
50a37d
         name = ""
50a37d
-        size = i32(read(4))
50a37d
+        size = i32(read(4))  # length of the extra data field
50a37d
         combined = 0
50a37d
         if size:
50a37d
+            data_end = file.tell() + size
50a37d
+
50a37d
             length = i32(read(4))
50a37d
             if length:
50a37d
                 mask_y = i32(read(4))
50a37d
@@ -233,7 +235,7 @@ def _layerinfo(file):
50a37d
                 name = read(length).decode('latin-1', 'replace')
50a37d
             combined += length + 1
50a37d
 
50a37d
-        file.seek(size - combined, 1)
50a37d
+            file.seek(data_end)
50a37d
         layers.append((name, mode, (x0, y0, x1, y1)))
50a37d
 
50a37d
     # get tiles
50a37d
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
50a37d
index f903918..b9a1ef7 100644
50a37d
--- a/src/PIL/TiffImagePlugin.py
50a37d
+++ b/src/PIL/TiffImagePlugin.py
50a37d
@@ -1170,8 +1170,8 @@ class TiffImageFile(ImageFile.ImageFile):
50a37d
             print("- fill_order:", fillorder)
50a37d
 
50a37d
         # size
50a37d
-        xsize = self.tag_v2.get(IMAGEWIDTH)
50a37d
-        ysize = self.tag_v2.get(IMAGELENGTH)
50a37d
+        xsize = int(self.tag_v2.get(IMAGEWIDTH))
50a37d
+        ysize = int(self.tag_v2.get(IMAGELENGTH))
50a37d
         self.size = xsize, ysize
50a37d
 
50a37d
         if DEBUG:
50a37d
diff --git a/src/libImaging/FliDecode.c b/src/libImaging/FliDecode.c
50a37d
index 6d22c6c..a99aca8 100644
50a37d
--- a/src/libImaging/FliDecode.c
50a37d
+++ b/src/libImaging/FliDecode.c
50a37d
@@ -30,7 +30,7 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
50a37d
 {
50a37d
     UINT8* ptr;
50a37d
     int framesize;
50a37d
-    int c, chunks;
50a37d
+    int c, chunks, advance;
50a37d
     int l, lines;
50a37d
     int i, j, x = 0, y, ymax;
50a37d
 
50a37d
@@ -59,10 +59,16 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
50a37d
 
50a37d
     chunks = I16(ptr+6);
50a37d
     ptr += 16;
50a37d
+	bytes -= 16;
50a37d
 
50a37d
     /* Process subchunks */
50a37d
     for (c = 0; c < chunks; c++) {
50a37d
-	UINT8 *data = ptr + 6;
50a37d
+	UINT8* data;
50a37d
+	if (bytes < 10) {
50a37d
+	    state->errcode = IMAGING_CODEC_OVERRUN;
50a37d
+	    return -1;
50a37d
+	}
50a37d
+	data = ptr + 6;
50a37d
 	switch (I16(ptr+4)) {
50a37d
 	case 4: case 11:
50a37d
 	    /* FLI COLOR chunk */
50a37d
@@ -198,7 +204,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
50a37d
 	    state->errcode = IMAGING_CODEC_UNKNOWN;
50a37d
 	    return -1;
50a37d
 	}
50a37d
-	ptr += I32(ptr);
50a37d
+	advance = I32(ptr);
50a37d
+	ptr += advance;
50a37d
+	bytes -= advance;
50a37d
     }
50a37d
 
50a37d
     return -1; /* end of frame */
50a37d
diff --git a/src/libImaging/PcxDecode.c b/src/libImaging/PcxDecode.c
50a37d
index e5417f1..aaf5867 100644
50a37d
--- a/src/libImaging/PcxDecode.c
50a37d
+++ b/src/libImaging/PcxDecode.c
50a37d
@@ -22,6 +22,14 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
50a37d
     UINT8 n;
50a37d
     UINT8* ptr;
50a37d
 
50a37d
+    if (strcmp(im->mode, "1") == 0 && state->xsize > state->bytes * 8) {
50a37d
+        state->errcode = IMAGING_CODEC_OVERRUN;
50a37d
+        return -1;
50a37d
+    } else if (strcmp(im->mode, "P") == 0 && state->xsize > state->bytes) {
50a37d
+        state->errcode = IMAGING_CODEC_OVERRUN;
50a37d
+        return -1;
50a37d
+    }
50a37d
+
50a37d
     ptr = buf;
50a37d
 
50a37d
     for (;;) {
50a37d
diff --git a/src/libImaging/RawDecode.c b/src/libImaging/RawDecode.c
50a37d
index 40c0cb7..d4b7994 100644
50a37d
--- a/src/libImaging/RawDecode.c
50a37d
+++ b/src/libImaging/RawDecode.c
50a37d
@@ -33,8 +33,15 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
50a37d
 
50a37d
 	/* get size of image data and padding */
50a37d
 	state->bytes = (state->xsize * state->bits + 7) / 8;
50a37d
-	rawstate->skip = (rawstate->stride) ?
50a37d
-	    rawstate->stride - state->bytes : 0;
50a37d
+	if (rawstate->stride) {
50a37d
+	    rawstate->skip = rawstate->stride - state->bytes;
50a37d
+	    if (rawstate->skip < 0) {
50a37d
+	        state->errcode = IMAGING_CODEC_CONFIG;
50a37d
+	        return -1;
50a37d
+	    }
50a37d
+	} else {
50a37d
+	    rawstate->skip = 0;
50a37d
+	}
50a37d
 
50a37d
 	/* check image orientation */
50a37d
 	if (state->ystep < 0) {
50a37d
diff --git a/src/libImaging/SgiRleDecode.c b/src/libImaging/SgiRleDecode.c
50a37d
index 9d8e563..39e7b3a 100644
50a37d
--- a/src/libImaging/SgiRleDecode.c
50a37d
+++ b/src/libImaging/SgiRleDecode.c
50a37d
@@ -156,6 +156,11 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
50a37d
             c->rlelength = c->lengthtab[c->rowno + c->channo * im->ysize];
50a37d
             c->rleoffset -= SGI_HEADER_SIZE;
50a37d
 
50a37d
+            if (c->rleoffset + c->rlelength > c->bufsize) {
50a37d
+                state->errcode = IMAGING_CODEC_OVERRUN;
50a37d
+                return -1;
50a37d
+            }
50a37d
+
50a37d
             /* row decompression */
50a37d
             if (c->bpc ==1) {
50a37d
                 if(expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands))
50a37d
-- 
50a37d
2.24.1
50a37d