Skip to content

Commit

Permalink
Catch SGI buffer overruns
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jan 2, 2020
1 parent 93b22b8 commit a79b65c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Binary file added Tests/images/sgi_overrun_expandrow.bin
Binary file not shown.
Binary file added Tests/images/sgi_overrun_expandrow2.bin
Binary file not shown.
2 changes: 2 additions & 0 deletions Tests/test_image.py
Expand Up @@ -593,6 +593,8 @@ def test_overrun(self):
for file in [
"fli_overrun.bin",
"sgi_overrun.bin",
"sgi_overrun_expandrow.bin",
"sgi_overrun_expandrow2.bin",
"pcx_overrun.bin",
"pcx_overrun2.bin",
]:
Expand Down
23 changes: 17 additions & 6 deletions src/libImaging/SgiRleDecode.c
Expand Up @@ -25,7 +25,7 @@ static void read4B(UINT32* dest, UINT8* buf)
*dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}

static int expandrow(UINT8* dest, UINT8* src, int n, int z)
static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
{
UINT8 pixel, count;

Expand All @@ -37,6 +37,9 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z)
count = pixel & RLE_MAX_RUN;
if (!count)
return count;
if (count > xsize) {
return -1;
}
if (pixel & RLE_COPY_FLAG) {
while(count--) {
*dest = *src++;
Expand All @@ -56,7 +59,7 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z)
return 0;
}

static int expandrow2(UINT8* dest, const UINT8* src, int n, int z)
static int expandrow2(UINT8* dest, const UINT8* src, int n, int z, int xsize)
{
UINT8 pixel, count;

Expand All @@ -70,6 +73,9 @@ static int expandrow2(UINT8* dest, const UINT8* src, int n, int z)
count = pixel & RLE_MAX_RUN;
if (!count)
return count;
if (count > xsize) {
return -1;
}
if (pixel & RLE_COPY_FLAG) {
while(count--) {
memcpy(dest, src, 2);
Expand All @@ -96,6 +102,7 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
UINT8 *ptr;
SGISTATE *c;
int err = 0;
int status;

/* Get all data from File descriptor */
c = (SGISTATE*)state->context;
Expand Down Expand Up @@ -164,12 +171,16 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state,

/* row decompression */
if (c->bpc ==1) {
if(expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands))
goto sgi_finish_decode;
status = expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
}
else {
if(expandrow2(&state->buffer[c->channo * 2], &ptr[c->rleoffset], c->rlelength, im->bands))
goto sgi_finish_decode;
status = expandrow2(&state->buffer[c->channo * 2], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
}
if (status == -1) {
state->errcode = IMAGING_CODEC_OVERRUN;
return -1;
} else if (status == 1) {
goto sgi_finish_decode;
}

state->count += c->rlelength;
Expand Down

1 comment on commit a79b65c

@NicoleG25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that CVE-2020-5311 was assigned to this issue.

Please sign in to comment.