699c67
From 1312c5426e7dd84e396ef2ff35aa09b64d92d382 Mon Sep 17 00:00:00 2001
699c67
From: Lumir Balhar <lbalhar@redhat.com>
699c67
Date: Fri, 9 Apr 2021 19:33:55 +0200
699c67
Subject: [PATCH 3/4] CVE-2021-25293
699c67
699c67
---
699c67
 src/libImaging/SgiRleDecode.c | 88 +++++++++++++++++++++++++++++------
699c67
 1 file changed, 75 insertions(+), 13 deletions(-)
699c67
699c67
diff --git a/src/libImaging/SgiRleDecode.c b/src/libImaging/SgiRleDecode.c
699c67
index 2259159..85af456 100644
699c67
--- a/src/libImaging/SgiRleDecode.c
699c67
+++ b/src/libImaging/SgiRleDecode.c
699c67
@@ -25,13 +25,60 @@ static void read4B(UINT32* dest, UINT8* buf)
699c67
     *dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
699c67
 }
699c67
 
699c67
-static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
699c67
+/*
699c67
+   SgiRleDecoding is done in a single channel row oriented set of RLE chunks.
699c67
+
699c67
+   * The file is arranged as
699c67
+     - SGI Header
699c67
+     - Rle Offset Table
699c67
+     - Rle Length Table
699c67
+     - Scanline Data
699c67
+
699c67
+   * Each RLE atom is c->bpc bytes wide (1 or 2)
699c67
+
699c67
+   * Each RLE Chunk is [specifier atom] [ 1 or n data atoms ]
699c67
+
699c67
+   * Copy Atoms are a byte with the high bit set, and the low 7 are
699c67
+     the number of bytes to copy from the source to the
699c67
+     destination. e.g.
699c67
+
699c67
+         CBBBBBBBB or 0CHLHLHLHLHLHL   (B=byte, H/L = Hi low bytes)
699c67
+
699c67
+   * Run atoms do not have the high bit set, and the low 7 bits are
699c67
+     the number of copies of the next atom to copy to the
699c67
+     destination. e.g.:
699c67
+
699c67
+         RB -> BBBBB or RHL -> HLHLHLHLHL
699c67
+
699c67
+   The upshot of this is, there is no way to determine the required
699c67
+   length of the input buffer from reloffset and rlelength without
699c67
+   going through the data at that scan line.
699c67
+
699c67
+   Furthermore, there's no requirement that individual scan lines
699c67
+   pointed to from the rleoffset table are in any sort of order or
699c67
+   used only once, or even disjoint. There's also no requirement that
699c67
+   all of the data in the scan line area of the image file be used
699c67
+
699c67
+ */
699c67
+
699c67
+static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize, UINT8 *end_of_buffer)
699c67
 {
699c67
+    /*
699c67
+     * n here is the number of rlechunks
699c67
+     * z is the number of channels, for calculating the interleave
699c67
+     *   offset to go to RGBA style pixels
699c67
+     * xsize is the row width
699c67
+     * end_of_buffer is the address of the end of the input buffer
699c67
+     */
699c67
+
699c67
     UINT8 pixel, count;
699c67
     int x = 0;
699c67
 
699c67
     for (;n > 0; n--)
699c67
     {
699c67
+        if (src > end_of_buffer) {
699c67
+            return -1;
699c67
+        }
699c67
         pixel = *src++;
699c67
         if (n == 1 && pixel != 0)
699c67
             return n;
699c67
@@ -43,6 +90,9 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
699c67
         }
699c67
         x += count;
699c67
         if (pixel & RLE_COPY_FLAG) {
699c67
+            if (src + count > end_of_buffer) {
699c67
+                return -1;
699c67
+            }
699c67
             while(count--) {
699c67
                 *dest = *src++;
699c67
                 dest += z;
699c67
@@ -50,6 +100,9 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
699c67
 
699c67
         }
699c67
         else {
699c67
+            if (src > end_of_buffer) {
699c67
+                return -1;
699c67
+            }
699c67
             pixel = *src++;
699c67
             while (count--) {
699c67
                 *dest = pixel;
699c67
@@ -61,7 +114,7 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
699c67
     return 0;
699c67
 }
699c67
 
699c67
-static int expandrow2(UINT16* dest, UINT16* src, int n, int z, int xsize)
699c67
+static int expandrow2(UINT16* dest, UINT16* src, int n, int z, int xsize, UINT8 *end_of_buffer)
699c67
 {
699c67
     UINT8 pixel, count;
699c67
 
699c67
@@ -69,6 +122,9 @@ static int expandrow2(UINT16* dest, UINT16* src, int n, int z, int xsize)
699c67
 
699c67
     for (;n > 0; n--)
699c67
     {
699c67
+        if (src + 1 > end_of_buffer) {
699c67
+            return -1;
699c67
+        }
699c67
         pixel = ((UINT8*)src)[1];
699c67
         ++src;
699c67
         if (n == 1 && pixel != 0)
699c67
@@ -81,12 +137,18 @@ static int expandrow2(UINT16* dest, UINT16* src, int n, int z, int xsize)
699c67
         }
699c67
         x += count;
699c67
         if (pixel & RLE_COPY_FLAG) {
699c67
+            if (src + 2 * count > end_of_buffer) {
699c67
+                return -1;
699c67
+            }
699c67
             while(count--) {
699c67
                 *dest = *src++;
699c67
                 dest += z;
699c67
             }
699c67
         }
699c67
         else {
699c67
+            if (src + 2 > end_of_buffer) {
699c67
+                return -1;
699c67
+            }
699c67
             while (count--) {
699c67
                 *dest = *src;
699c67
                 dest += z;
699c67
@@ -136,8 +198,10 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
699c67
         return -1;
699c67
     }
699c67
     _imaging_seek_pyFd(state->fd, SGI_HEADER_SIZE, SEEK_SET);
699c67
-    _imaging_read_pyFd(state->fd, (char*)ptr, c->bufsize);
699c67
-
699c67
+    if (_imaging_read_pyFd(state->fd, (char *)ptr, c->bufsize) != c->bufsize) {
699c67
+        state->errcode = IMAGING_CODEC_UNKNOWN;
699c67
+        return -1;
699c67
+    }
699c67
 
699c67
     /* decoder initialization */
699c67
     state->count = 0;
699c67
@@ -168,8 +232,6 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
699c67
     for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(UINT32); c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4)
699c67
         read4B(&c->lengthtab[c->tabindex], &ptr[c->bufindex]);
699c67
 
699c67
-    state->count += c->tablen * sizeof(UINT32) * 2;
699c67
-
699c67
     /* read compressed rows */
699c67
     for (c->rowno = 0; c->rowno < im->ysize; c->rowno++, state->y += state->ystep)
699c67
     {
699c67
@@ -177,19 +239,21 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
699c67
         {
699c67
             c->rleoffset = c->starttab[c->rowno + c->channo * im->ysize];
699c67
             c->rlelength = c->lengthtab[c->rowno + c->channo * im->ysize];
699c67
-            c->rleoffset -= SGI_HEADER_SIZE;
699c67
 
699c67
-            if (c->rleoffset + c->rlelength > c->bufsize) {
699c67
+            // Check for underflow of rleoffset-SGI_HEADER_SIZE
699c67
+            if (c->rleoffset < SGI_HEADER_SIZE) {
699c67
                 state->errcode = IMAGING_CODEC_OVERRUN;
699c67
                 goto sgi_finish_decode;
699c67
             }
699c67
 
699c67
+            c->rleoffset -= SGI_HEADER_SIZE;
699c67
+
699c67
             /* row decompression */
699c67
             if (c->bpc ==1) {
699c67
-                status = expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
699c67
+                status = expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize, &ptr[c->bufsize-1]);
699c67
             }
699c67
             else {
699c67
-                status = expandrow2(&state->buffer[c->channo * 2], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
699c67
+                status = expandrow2(&state->buffer[c->channo * 2], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize, &ptr[c->bufsize-1]);
699c67
             }
699c67
             if (status == -1) {
699c67
                 state->errcode = IMAGING_CODEC_OVERRUN;
699c67
@@ -198,7 +262,6 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
699c67
                 goto sgi_finish_decode;
699c67
             }
699c67
 
699c67
-            state->count += c->rlelength;
699c67
         }
699c67
 
699c67
         /* store decompressed data in image */
699c67
@@ -206,7 +269,6 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
699c67
 
699c67
     }
699c67
 
699c67
-    c->bufsize++;
699c67
 
699c67
 sgi_finish_decode: ;
699c67
 
699c67
@@ -217,5 +279,5 @@ sgi_finish_decode: ;
699c67
         state->errcode=err;
699c67
         return -1;
699c67
     }
699c67
-    return state->count - c->bufsize;
699c67
+    return 0;
699c67
 }
699c67
-- 
699c67
2.30.2
699c67