File 020-CVE-2020-35653.patch of Package python-Pillow
From 2f409261eb1228e166868f8f0b5da5cda52e55bf Mon Sep 17 00:00:00 2001
From: Eric Soroos <eric-github@soroos.net>
Date: Thu, 17 Dec 2020 00:17:53 +0100
Subject: [PATCH 1/2] Fix for CVE CVE-2020-35655 - Read Overflow in PCX
Decoding.
* Don't trust the image to specify a buffer size
---
Tests/images/ossfuzz-4836216264589312.pcx | Bin 0 -> 129 bytes
Tests/test_image.py | 27 ++++++++++++----------
src/PIL/PcxImagePlugin.py | 9 ++++++--
3 files changed, 22 insertions(+), 14 deletions(-)
create mode 100644 Tests/images/ossfuzz-4836216264589312.pcx
diff --git a/Tests/test_image.py b/Tests/test_image.py
index 84c098cc8a4..d91f1c263f8 100644
--- a/Tests/test_image.py
+++ b/Tests/test_image.py
@@ -545,6 +545,7 @@ class TestImage(PillowTestCase):
"sgi_overrun_expandrow2.bin",
"pcx_overrun.bin",
"pcx_overrun2.bin",
+ "ossfuzz-4836216264589312.pcx",
"01r_00.pcx",
]:
im = Image.open(os.path.join("Tests/images", file))
@@ -552,7 +553,9 @@ class TestImage(PillowTestCase):
im.load()
self.assertFail()
except IOError as e:
- self.assertEqual(str(e), "buffer overrun when reading image file")
+ buffer_overrun = str(e) == "buffer overrun when reading image file"
+ truncated = "image file is truncated" in str(e)
+ self.assertTrue(buffer_overrun or truncated)
with Image.open("Tests/images/fli_overrun2.bin") as im:
try:
diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py
index b337b7dde20..a24d44b4278 100644
--- a/src/PIL/PcxImagePlugin.py
+++ b/src/PIL/PcxImagePlugin.py
@@ -63,9 +63,9 @@ class PcxImageFile(ImageFile.ImageFile):
version = i8(s[1])
bits = i8(s[3])
planes = i8(s[65])
- stride = i16(s, 66)
+ ignored_stride = i16(s, 66)
logger.debug("PCX version %s, bits %s, planes %s, stride %s",
- version, bits, planes, stride)
+ version, bits, planes, ignored_stride)
self.info["dpi"] = i16(s, 12), i16(s, 14)
@@ -102,6 +102,11 @@ class PcxImageFile(ImageFile.ImageFile):
self.mode = mode
self.size = bbox[2]-bbox[0], bbox[3]-bbox[1]
+ # don't trust the passed in stride. Calculate for ourselves.
+ # CVE-2020-35655
+ stride = (self.size[0] * bits + 7) // 8
+ stride += stride % 2
+
bbox = (0, 0) + self.size
logger.debug("size: %sx%s", *self.size)