b8ba79
From 1dd0fb64bd3cc221b5877ece4ce2f300245b638f Mon Sep 17 00:00:00 2001
b8ba79
From: Lumir Balhar <lbalhar@redhat.com>
b8ba79
Date: Mon, 17 Feb 2020 14:19:32 +0100
b8ba79
Subject: [PATCH] CVE-2020-5311
b8ba79
b8ba79
---
b8ba79
 src/libImaging/SgiRleDecode.c | 23 +++++++++++++++++------
b8ba79
 1 file changed, 17 insertions(+), 6 deletions(-)
b8ba79
b8ba79
diff --git a/src/libImaging/SgiRleDecode.c b/src/libImaging/SgiRleDecode.c
b8ba79
index 39e7b3a..6367ae7 100644
b8ba79
--- a/src/libImaging/SgiRleDecode.c
b8ba79
+++ b/src/libImaging/SgiRleDecode.c
b8ba79
@@ -25,7 +25,7 @@ static void read4B(UINT32* dest, UINT8* buf)
b8ba79
     *dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
b8ba79
 }
b8ba79
 
b8ba79
-static int expandrow(UINT8* dest, UINT8* src, int n, int z)
b8ba79
+static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
b8ba79
 {
b8ba79
     UINT8 pixel, count;
b8ba79
 
b8ba79
@@ -37,6 +37,9 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z)
b8ba79
         count = pixel & RLE_MAX_RUN;
b8ba79
         if (!count)
b8ba79
             return count;
b8ba79
+        if (count > xsize) {
b8ba79
+            return -1;
b8ba79
+        }
b8ba79
         if (pixel & RLE_COPY_FLAG) {
b8ba79
             while(count--) {
b8ba79
                 *dest = *src++;
b8ba79
@@ -56,7 +59,7 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z)
b8ba79
     return 0;
b8ba79
 }
b8ba79
 
b8ba79
-static int expandrow2(UINT16* dest, UINT16* src, int n, int z)
b8ba79
+static int expandrow2(UINT16* dest, UINT16* src, int n, int z, int xsize)
b8ba79
 {
b8ba79
     UINT8 pixel, count;
b8ba79
 
b8ba79
@@ -70,6 +73,9 @@ static int expandrow2(UINT16* dest, UINT16* src, int n, int z)
b8ba79
         count = pixel & RLE_MAX_RUN;
b8ba79
         if (!count)
b8ba79
             return count;
b8ba79
+        if (count > xsize) {
b8ba79
+            return -1;
b8ba79
+        }
b8ba79
         if (pixel & RLE_COPY_FLAG) {
b8ba79
             while(count--) {
b8ba79
                 *dest = *src++;
b8ba79
@@ -95,6 +101,7 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
b8ba79
     UINT8 *ptr;
b8ba79
     SGISTATE *c;
b8ba79
     int err = 0;
b8ba79
+    int status;
b8ba79
 
b8ba79
     /* Get all data from File descriptor */
b8ba79
     c = (SGISTATE*)state->context;
b8ba79
@@ -163,12 +170,16 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
b8ba79
 
b8ba79
             /* row decompression */
b8ba79
             if (c->bpc ==1) {
b8ba79
-                if(expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands))
b8ba79
-                    goto sgi_finish_decode;
b8ba79
+                status = expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
b8ba79
             }
b8ba79
             else {
b8ba79
-                if(expandrow2((UINT16*)&state->buffer[c->channo * 2], (UINT16*)&ptr[c->rleoffset], c->rlelength, im->bands))
b8ba79
-                    goto sgi_finish_decode;
b8ba79
+                status = expandrow2(&state->buffer[c->channo * 2], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
b8ba79
+            }
b8ba79
+            if (status == -1) {
b8ba79
+                state->errcode = IMAGING_CODEC_OVERRUN;
b8ba79
+                return -1;
b8ba79
+            } else if (status == 1) {
b8ba79
+                goto sgi_finish_decode;
b8ba79
             }
b8ba79
 
b8ba79
             state->count += c->rlelength;
b8ba79
-- 
b8ba79
2.24.1
b8ba79