e773f2
From 7a0aea5806d57e0e7c5187fbc9c2937a16e0bca1 Mon Sep 17 00:00:00 2001
e773f2
From: Eric Soroos <eric-github@soroos.net>
e773f2
Date: Thu, 17 Dec 2020 00:17:53 +0100
e773f2
Subject: [PATCH] Fix for CVE CVE-2020-35655 - Read Overflow in PCX Decoding.
e773f2
e773f2
* Don't trust the image to specify a buffer size
e773f2
---
e773f2
 src/PIL/PcxImagePlugin.py | 9 +++++++--
e773f2
 1 file changed, 7 insertions(+), 2 deletions(-)
e773f2
e773f2
diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py
e773f2
index 564713a..17bbd18 100644
e773f2
--- a/src/PIL/PcxImagePlugin.py
e773f2
+++ b/src/PIL/PcxImagePlugin.py
e773f2
@@ -63,9 +63,9 @@ class PcxImageFile(ImageFile.ImageFile):
e773f2
         version = i8(s[1])
e773f2
         bits = i8(s[3])
e773f2
         planes = i8(s[65])
e773f2
-        stride = i16(s, 66)
e773f2
+        ignored_stride = i16(s, 66)
e773f2
         logger.debug("PCX version %s, bits %s, planes %s, stride %s",
e773f2
-                     version, bits, planes, stride)
e773f2
+                     version, bits, planes, ignored_stride)
e773f2
 
e773f2
         self.info["dpi"] = i16(s, 12), i16(s, 14)
e773f2
 
e773f2
@@ -102,6 +102,11 @@ class PcxImageFile(ImageFile.ImageFile):
e773f2
         self.mode = mode
e773f2
         self.size = bbox[2]-bbox[0], bbox[3]-bbox[1]
e773f2
 
e773f2
+        # don't trust the passed in stride. Calculate for ourselves.
e773f2
+        # CVE-2020-35653
e773f2
+        stride = (self.size[0] * bits + 7) // 8
e773f2
+        stride += stride % 2
e773f2
+
e773f2
         bbox = (0, 0) + self.size
e773f2
         logger.debug("size: %sx%s", *self.size)
e773f2
 
e773f2
-- 
e773f2
2.29.2
e773f2