bd9949
From 6cad2f6857ab669139e5d417139b5795fae4afdf Mon Sep 17 00:00:00 2001
bd9949
From: Lumir Balhar <lbalhar@redhat.com>
bd9949
Date: Fri, 14 Feb 2020 09:43:57 +0100
bd9949
Subject: [PATCH] CVE-2020-5312_CVE-2019-16865
bd9949
bd9949
---
bd9949
 .../PIL/PsdImagePlugin.py                          |  6 ++++--
bd9949
 .../libImaging/FliDecode.c                         | 14 +++++++++++---
bd9949
 .../libImaging/PcxDecode.c                         |  8 ++++++++
bd9949
 .../libImaging/RawDecode.c                         | 11 +++++++++--
bd9949
 4 files changed, 32 insertions(+), 7 deletions(-)
bd9949
bd9949
diff --git a/python-imaging-Pillow-d1c6db8/PIL/PsdImagePlugin.py b/python-imaging-Pillow-d1c6db8/PIL/PsdImagePlugin.py
bd9949
index 2192015..9072875 100644
bd9949
--- a/python-imaging-Pillow-d1c6db8/PIL/PsdImagePlugin.py
bd9949
+++ b/python-imaging-Pillow-d1c6db8/PIL/PsdImagePlugin.py
bd9949
@@ -202,9 +202,11 @@ def _layerinfo(file):
bd9949
         # skip over blend flags and extra information
bd9949
         filler = read(12)
bd9949
         name = ""
bd9949
-        size = i32(read(4))
bd9949
+        size = i32(read(4))  # length of the extra data field
bd9949
         combined = 0
bd9949
         if size:
bd9949
+            data_end = file.tell() + size
bd9949
+
bd9949
             length = i32(read(4))
bd9949
             if length:
bd9949
                 mask_y = i32(read(4)); mask_x = i32(read(4))
bd9949
@@ -223,7 +225,7 @@ def _layerinfo(file):
bd9949
                 name = read(length).decode('latin-1', 'replace')
bd9949
             combined += length + 1
bd9949
 
bd9949
-        file.seek(size - combined, 1)
bd9949
+            file.seek(data_end)
bd9949
         layers.append((name, mode, (x0, y0, x1, y1)))
bd9949
 
bd9949
     # get tiles
bd9949
diff --git a/python-imaging-Pillow-d1c6db8/libImaging/FliDecode.c b/python-imaging-Pillow-d1c6db8/libImaging/FliDecode.c
bd9949
index 75eebe8..940171f 100644
bd9949
--- a/python-imaging-Pillow-d1c6db8/libImaging/FliDecode.c
bd9949
+++ b/python-imaging-Pillow-d1c6db8/libImaging/FliDecode.c
bd9949
@@ -30,7 +30,7 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
bd9949
 {
bd9949
     UINT8* ptr;
bd9949
     int framesize;
bd9949
-    int c, chunks;
bd9949
+    int c, chunks, advance;
bd9949
     int l, lines;
bd9949
     int i, j, x = 0, y, ymax;
bd9949
 
bd9949
@@ -59,10 +59,16 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
bd9949
 
bd9949
     chunks = I16(ptr+6);
bd9949
     ptr += 16;
bd9949
+	bytes -= 16;
bd9949
 
bd9949
     /* Process subchunks */
bd9949
     for (c = 0; c < chunks; c++) {
bd9949
-	UINT8 *data = ptr + 6;
bd9949
+	UINT8* data;
bd9949
+	if (bytes < 10) {
bd9949
+	    state->errcode = IMAGING_CODEC_OVERRUN;
bd9949
+	    return -1;
bd9949
+	}
bd9949
+	data = ptr + 6;
bd9949
 	switch (I16(ptr+4)) {
bd9949
 	case 4: case 11:
bd9949
 	    /* FLI COLOR chunk */
bd9949
@@ -198,7 +204,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
bd9949
 	    state->errcode = IMAGING_CODEC_UNKNOWN;
bd9949
 	    return -1;
bd9949
 	}
bd9949
-	ptr += I32(ptr);
bd9949
+	advance = I32(ptr);
bd9949
+	ptr += advance;
bd9949
+	bytes -= advance;
bd9949
     }
bd9949
 
bd9949
     return -1; /* end of frame */
bd9949
diff --git a/python-imaging-Pillow-d1c6db8/libImaging/PcxDecode.c b/python-imaging-Pillow-d1c6db8/libImaging/PcxDecode.c
bd9949
index ab82b23..4a1d92a 100644
bd9949
--- a/python-imaging-Pillow-d1c6db8/libImaging/PcxDecode.c
bd9949
+++ b/python-imaging-Pillow-d1c6db8/libImaging/PcxDecode.c
bd9949
@@ -22,6 +22,14 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
bd9949
     UINT8 n;
bd9949
     UINT8* ptr;
bd9949
 
bd9949
+    if (strcmp(im->mode, "1") == 0 && state->xsize > state->bytes * 8) {
bd9949
+        state->errcode = IMAGING_CODEC_OVERRUN;
bd9949
+        return -1;
bd9949
+    } else if (strcmp(im->mode, "P") == 0 && state->xsize > state->bytes) {
bd9949
+        state->errcode = IMAGING_CODEC_OVERRUN;
bd9949
+        return -1;
bd9949
+    }
bd9949
+
bd9949
     ptr = buf;
bd9949
 
bd9949
     for (;;) {
bd9949
diff --git a/python-imaging-Pillow-d1c6db8/libImaging/RawDecode.c b/python-imaging-Pillow-d1c6db8/libImaging/RawDecode.c
bd9949
index 5aadb2b..b8b667e 100644
bd9949
--- a/python-imaging-Pillow-d1c6db8/libImaging/RawDecode.c
bd9949
+++ b/python-imaging-Pillow-d1c6db8/libImaging/RawDecode.c
bd9949
@@ -33,8 +33,15 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
bd9949
 
bd9949
 	/* get size of image data and padding */
bd9949
 	state->bytes = (state->xsize * state->bits + 7) / 8;
bd9949
-	rawstate->skip = (rawstate->stride) ?
bd9949
-	    rawstate->stride - state->bytes : 0;
bd9949
+	if (rawstate->stride) {
bd9949
+	    rawstate->skip = rawstate->stride - state->bytes;
bd9949
+	    if (rawstate->skip < 0) {
bd9949
+	        state->errcode = IMAGING_CODEC_CONFIG;
bd9949
+	        return -1;
bd9949
+	    }
bd9949
+	} else {
bd9949
+	    rawstate->skip = 0;
bd9949
+	}
bd9949
 
bd9949
 	/* check image orientation */
bd9949
 	if (state->ystep < 0) {
bd9949
-- 
bd9949
2.24.1
bd9949