Fixed bug 4500 - Heap-Buffer Overflow in Map1toN pertaining to SDL_pixels.c
authorSam Lantinga <slouken@libsdl.org>
Mon, 18 Feb 2019 07:50:33 -0800
changeset 1261207c39cbbeacf
parent 12610 587922004ea0
child 12613 c8f998f83225
Fixed bug 4500 - Heap-Buffer Overflow in Map1toN pertaining to SDL_pixels.c

Petr Pisar

The reproducer has these data in BITMAPINFOHEADER:

biSize = 40
biBitCount = 8
biClrUsed = 131075

SDL_LoadBMP_RW() function passes biBitCount as a color depth to SDL_CreateRGBSurface(), thus 256-color pallete is allocated. But then biClrUsed colors are read from a file and stored into the palette. SDL_LoadBMP_RW should report an error if biClrUsed is greater than 2^biBitCount.
src/video/SDL_bmp.c
     1.1 --- a/src/video/SDL_bmp.c	Sun Feb 17 16:20:23 2019 +0100
     1.2 +++ b/src/video/SDL_bmp.c	Mon Feb 18 07:50:33 2019 -0800
     1.3 @@ -313,6 +313,10 @@
     1.4          SDL_assert(biBitCount <= 8);
     1.5          if (biClrUsed == 0) {
     1.6              biClrUsed = 1 << biBitCount;
     1.7 +		} else if (biClrUsed > (1 << biBitCount)) {
     1.8 +			SDL_SetError("BMP file has an invalid number of colors");
     1.9 +			was_error = SDL_TRUE;
    1.10 +			goto done;
    1.11          }
    1.12          if ((int) biClrUsed > palette->ncolors) {
    1.13              SDL_Color *colors;