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