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