diff -Nru allegro5-5.2.6.0/debian/changelog allegro5-5.2.6.0/debian/changelog --- allegro5-5.2.6.0/debian/changelog 2020-11-27 13:02:04.000000000 +0100 +++ allegro5-5.2.6.0/debian/changelog 2024-03-20 21:55:45.000000000 +0100 @@ -1,3 +1,11 @@ +allegro5 (2:5.2.6.0-3+deb11u1) bullseye; urgency=medium + + * CVE-2021-36489 - Buffer Overflow vulnerability in Allegro through + 5.2.6 allows attackers to cause a denial of service via crafted + PCX/TGA/BMP files to allegro_image addon. + + -- Andreas Rönnquist Wed, 20 Mar 2024 21:55:45 +0100 + allegro5 (2:5.2.6.0-3) unstable; urgency=medium * Add patch to use GTK3 instead of GTK2 (Closes: #967247) diff -Nru allegro5-5.2.6.0/debian/patches/0006-Prevent-RLE-data-in-corrupt-.tga-from-overflowing-th.patch allegro5-5.2.6.0/debian/patches/0006-Prevent-RLE-data-in-corrupt-.tga-from-overflowing-th.patch --- allegro5-5.2.6.0/debian/patches/0006-Prevent-RLE-data-in-corrupt-.tga-from-overflowing-th.patch 1970-01-01 01:00:00.000000000 +0100 +++ allegro5-5.2.6.0/debian/patches/0006-Prevent-RLE-data-in-corrupt-.tga-from-overflowing-th.patch 2024-03-20 21:33:02.000000000 +0100 @@ -0,0 +1,323 @@ +From: Peter Hull +Date: Tue, 8 Jun 2021 20:37:14 +0100 +Applied-Upstream: https://github.com/liballeg/allegro5/commit/3f2dbd494241774d33aaf83910fd05b2a590604a +Subject: Prevent RLE data in corrupt .tga from overflowing the buffer + +Also masked out the 1-bit A value from a 15-bit image; this could +cause the bounds of _al_rgb_scale_5[] to be exceeded. +--- + addons/image/tga.c | 116 ++++++++++++++++++++++++++++++++++++++++++++--------- + 1 file changed, 96 insertions(+), 20 deletions(-) + +diff --git a/addons/image/tga.c b/addons/image/tga.c +index ec536b9..38b5800 100644 +--- a/addons/image/tga.c ++++ b/addons/image/tga.c +@@ -13,7 +13,7 @@ + * By Tim Gunn. + * + * RLE support added by Michal Mertl and Salvador Eduardo Tropea. +- * ++ * + * Palette reading improved by Peter Wang. + * + * Big-endian support added by Eric Botcazou. +@@ -34,6 +34,7 @@ ALLEGRO_DEBUG_CHANNEL("image") + + /* raw_tga_read8: + * Helper for reading 256-color raw data from TGA files. ++ * Returns pointer past the end. + */ + static INLINE unsigned char *raw_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f) + { +@@ -44,8 +45,9 @@ static INLINE unsigned char *raw_tga_read8(unsigned char *b, int w, ALLEGRO_FILE + + /* rle_tga_read8: + * Helper for reading 256-color RLE data from TGA files. ++ * Returns pointer past the end or NULL for error. + */ +-static void rle_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f) ++static unsigned char *rle_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f) + { + int value, count, c = 0; + +@@ -55,6 +57,10 @@ static void rle_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f) + /* run-length packet */ + count = (count & 0x7F) + 1; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + value = al_fgetc(f); + while (count--) + *b++ = value; +@@ -63,9 +69,14 @@ static void rle_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f) + /* raw packet */ + count++; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + b = raw_tga_read8(b, count, f); + } + } while (c < w); ++ return b; + } + + +@@ -82,6 +93,7 @@ static INLINE int32_t single_tga_read32(ALLEGRO_FILE *f) + + /* raw_tga_read32: + * Helper for reading 32-bit raw data from TGA files. ++ * Returns pointer past the end. + */ + static unsigned int *raw_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f) + { +@@ -95,8 +107,9 @@ static unsigned int *raw_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f) + + /* rle_tga_read32: + * Helper for reading 32-bit RLE data from TGA files. ++ * Returns pointer past the end or NULL for error. + */ +-static void rle_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f) ++static unsigned int *rle_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f) + { + int color, count, c = 0; + +@@ -106,6 +119,10 @@ static void rle_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f) + /* run-length packet */ + count = (count & 0x7F) + 1; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + color = single_tga_read32(f); + while (count--) + *b++ = color; +@@ -114,9 +131,14 @@ static void rle_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f) + /* raw packet */ + count++; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + b = raw_tga_read32(b, count, f); + } + } while (c < w); ++ return b; + } + + +@@ -132,6 +154,7 @@ static INLINE void single_tga_read24(ALLEGRO_FILE *f, unsigned char color[3]) + + /* raw_tga_read24: + * Helper for reading 24-bit raw data from TGA files. ++ * Returns pointer past the end. + */ + static unsigned char *raw_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f) + { +@@ -147,8 +170,9 @@ static unsigned char *raw_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f) + + /* rle_tga_read24: + * Helper for reading 24-bit RLE data from TGA files. ++ * Returns pointer past the end or NULL for error. + */ +-static void rle_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f) ++static unsigned char *rle_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f) + { + int count, c = 0; + unsigned char color[3]; +@@ -159,6 +183,10 @@ static void rle_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f) + /* run-length packet */ + count = (count & 0x7F) + 1; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + single_tga_read24(f, color); + while (count--) { + b[0] = color[0]; +@@ -171,9 +199,14 @@ static void rle_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f) + /* raw packet */ + count++; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + b = raw_tga_read24(b, count, f); + } + } while (c < w); ++ return b; + } + + +@@ -190,6 +223,7 @@ static INLINE int single_tga_read16(ALLEGRO_FILE *f) + + /* raw_tga_read16: + * Helper for reading 16-bit raw data from TGA files. ++ * Returns pointer past the end. + */ + static unsigned short *raw_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + { +@@ -203,8 +237,9 @@ static unsigned short *raw_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + + /* rle_tga_read16: + * Helper for reading 16-bit RLE data from TGA files. ++ * Returns pointer past the end or NULL for error. + */ +-static void rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) ++static unsigned short *rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + { + int color, count, c = 0; + +@@ -214,6 +249,10 @@ static void rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + /* run-length packet */ + count = (count & 0x7F) + 1; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + color = single_tga_read16(f); + while (count--) + *b++ = color; +@@ -222,9 +261,14 @@ static void rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + /* raw packet */ + count++; + c += count; ++ if (c > w) { ++ /* Stepped past the end of the line, error */ ++ return NULL; ++ } + b = raw_tga_read16(b, count, f); + } + } while (c < w); ++ return b; + } + + +@@ -388,12 +432,20 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + switch (image_type) { + + case 1: +- case 3: ++ case 3: { ++ unsigned char *ptr; + if (compressed) +- rle_tga_read8(buf, image_width, f); ++ ptr = rle_tga_read8(buf, image_width, f); + else +- raw_tga_read8(buf, image_width, f); +- ++ ptr = raw_tga_read8(buf, image_width, f); ++ if (!ptr) { ++ al_free(buf); ++ al_unlock_bitmap(bmp); ++ al_destroy_bitmap(bmp); ++ ALLEGRO_ERROR("Invalid image data.\n"); ++ return NULL; ++ } ++ } + for (i = 0; i < image_width; i++) { + int true_x = (left_to_right) ? i : (image_width - 1 - i); + int pix = buf[i]; +@@ -410,11 +462,18 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + + case 2: + if (bpp == 32) { ++ unsigned int *ptr; + if (compressed) +- rle_tga_read32((unsigned int *)buf, image_width, f); ++ ptr = rle_tga_read32((unsigned int *)buf, image_width, f); + else +- raw_tga_read32((unsigned int *)buf, image_width, f); +- ++ ptr = raw_tga_read32((unsigned int *)buf, image_width, f); ++ if (!ptr) { ++ al_free(buf); ++ al_unlock_bitmap(bmp); ++ al_destroy_bitmap(bmp); ++ ALLEGRO_ERROR("Invalid image data.\n"); ++ return NULL; ++ } + for (i = 0; i < image_width; i++) { + int true_x = (left_to_right) ? i : (image_width - 1 - i); + unsigned char *dest = (unsigned char *)lr->data + +@@ -425,12 +484,12 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + int r = buf[i * 4 + 1]; + int g = buf[i * 4 + 2]; + int b = buf[i * 4 + 3]; +-#else ++#else + int b = buf[i * 4 + 0]; + int g = buf[i * 4 + 1]; + int r = buf[i * 4 + 2]; + int a = buf[i * 4 + 3]; +-#endif ++#endif + if (premul) { + r = r * a / 255; + g = g * a / 255; +@@ -444,10 +503,18 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + } + } + else if (bpp == 24) { ++ unsigned char *ptr; + if (compressed) +- rle_tga_read24(buf, image_width, f); ++ ptr = rle_tga_read24(buf, image_width, f); + else +- raw_tga_read24(buf, image_width, f); ++ ptr = raw_tga_read24(buf, image_width, f); ++ if (!ptr) { ++ al_free(buf); ++ al_unlock_bitmap(bmp); ++ al_destroy_bitmap(bmp); ++ ALLEGRO_ERROR("Invalid image data.\n"); ++ return NULL; ++ } + for (i = 0; i < image_width; i++) { + int true_x = (left_to_right) ? i : (image_width - 1 - i); + int b = buf[i * 3 + 0]; +@@ -463,14 +530,23 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + } + } + else { ++ unsigned short *ptr; + if (compressed) +- rle_tga_read16((unsigned short *)buf, image_width, f); ++ ptr = rle_tga_read16((unsigned short *)buf, image_width, f); + else +- raw_tga_read16((unsigned short *)buf, image_width, f); ++ ptr = raw_tga_read16((unsigned short *)buf, image_width, f); ++ if (!ptr) { ++ al_free(buf); ++ al_unlock_bitmap(bmp); ++ al_destroy_bitmap(bmp); ++ ALLEGRO_ERROR("Invalid image data.\n"); ++ return NULL; ++ } + for (i = 0; i < image_width; i++) { + int true_x = (left_to_right) ? i : (image_width - 1 - i); + int pix = *((unsigned short *)(buf + i * 2)); +- int r = _al_rgb_scale_5[(pix >> 10)]; ++ /* TODO - do something with the 1-bit A value (alpha?) */ ++ int r = _al_rgb_scale_5[(pix >> 10) & 0x1F]; + int g = _al_rgb_scale_5[(pix >> 5) & 0x1F]; + int b = _al_rgb_scale_5[(pix & 0x1F)]; + +@@ -597,7 +673,7 @@ bool _al_identify_tga(ALLEGRO_FILE *f) + uint8_t x[4]; + al_fgetc(f); // skip id length + al_fread(f, x, 4); +- ++ + if (x[0] > 1) // TGA colormap must be 0 or 1 + return false; + if ((x[1] & 0xf7) == 0) // type must be 1, 2, 3, 9, 10 or 11 diff -Nru allegro5-5.2.6.0/debian/patches/0007-Reject-creating-bitmaps-with-negative-width-or-heigh.patch allegro5-5.2.6.0/debian/patches/0007-Reject-creating-bitmaps-with-negative-width-or-heigh.patch --- allegro5-5.2.6.0/debian/patches/0007-Reject-creating-bitmaps-with-negative-width-or-heigh.patch 1970-01-01 01:00:00.000000000 +0100 +++ allegro5-5.2.6.0/debian/patches/0007-Reject-creating-bitmaps-with-negative-width-or-heigh.patch 2024-03-20 21:33:02.000000000 +0100 @@ -0,0 +1,37 @@ +From: Peter Hull +Date: Sat, 12 Jun 2021 10:19:59 +0100 +Applied-Upstream: https://github.com/liballeg/allegro5/commit/0294e28e6135292eab4b2916a7d2223b1bb6843e +Subject: Reject creating bitmaps with negative width or height + +Also reject absurd sizes. Previously bitmaps with a height of +INT_MIN could slip through +the checks, by a combination of lower bounds and arithmetic +overflows and be created by the Open GL driver. However this bitmap +would cause a crash when al_lock_bitmap was called. +--- + src/bitmap.c | 9 ++++----- + 1 file changed, 4 insertions(+), 5 deletions(-) + +diff --git a/src/bitmap.c b/src/bitmap.c +index 77c76c2..b553c9e 100644 +--- a/src/bitmap.c ++++ b/src/bitmap.c +@@ -100,14 +100,13 @@ ALLEGRO_BITMAP *_al_create_bitmap_params(ALLEGRO_DISPLAY *current_display, + ALLEGRO_SYSTEM *system = al_get_system_driver(); + ALLEGRO_BITMAP *bitmap; + ALLEGRO_BITMAP **back; +- int64_t mul; + bool result; +- +- /* Reject bitmaps where a calculation pixel_size*w*h would overflow ++ /* Reject bitmaps with negative dimensions. ++ * Also reject bitmaps where a calculation pixel_size*w*h would overflow + * int. Supporting such bitmaps would require a lot more work. ++ * Overflow calc based on https://stackoverflow.com/a/1514309/231929 + */ +- mul = 4 * (int64_t) w * (int64_t) h; +- if (mul > (int64_t) INT_MAX) { ++ if (w < 0 || h < 0 || (int64_t) w > (INT_MAX/4) / (int64_t) h) { + ALLEGRO_WARN("Rejecting %dx%d bitmap\n", w, h); + return NULL; + } diff -Nru allegro5-5.2.6.0/debian/patches/0008-PCX-loader-validation.patch allegro5-5.2.6.0/debian/patches/0008-PCX-loader-validation.patch --- allegro5-5.2.6.0/debian/patches/0008-PCX-loader-validation.patch 1970-01-01 01:00:00.000000000 +0100 +++ allegro5-5.2.6.0/debian/patches/0008-PCX-loader-validation.patch 2024-03-20 21:33:02.000000000 +0100 @@ -0,0 +1,171 @@ +From: Peter Hull +Date: Mon, 14 Jun 2021 21:43:17 +0100 +Applied-Upstream: https://github.com/liballeg/allegro5/commit/cca179bc16827f358153060cd10ac73d394e758c +Subject: PCX loader validation + +Validate bytes_per_line. + +Validate palette +--- + addons/image/pcx.c | 5 ++++ + addons/image/tga.c | 86 ++++++++++++++++++++++++++++++++---------------------- + 2 files changed, 56 insertions(+), 35 deletions(-) + +diff --git a/addons/image/pcx.c b/addons/image/pcx.c +index d9464ea..08016e4 100644 +--- a/addons/image/pcx.c ++++ b/addons/image/pcx.c +@@ -54,6 +54,11 @@ ALLEGRO_BITMAP *_al_load_pcx_f(ALLEGRO_FILE *f, int flags) + } + + bytes_per_line = al_fread16le(f); ++ /* It can only be this because we only support 8 bit planes */ ++ if (bytes_per_line != width) { ++ ALLEGRO_ERROR("Invalid bytes per line %d\n", bytes_per_line); ++ return NULL; ++ } + + for (c = 0; c < 60; c++) /* skip some more junk */ + al_fgetc(f); +diff --git a/addons/image/tga.c b/addons/image/tga.c +index 38b5800..374f8de 100644 +--- a/addons/image/tga.c ++++ b/addons/image/tga.c +@@ -271,6 +271,7 @@ static unsigned short *rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + return b; + } + ++typedef unsigned char palette_entry[3]; + + /* Like load_tga, but starts loading from the current place in the ALLEGRO_FILE + * specified. If successful the offset into the file will be left just after +@@ -280,10 +281,12 @@ static unsigned short *rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f) + */ + ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + { +- unsigned char image_id[256], image_palette[256][3]; ++ unsigned char image_id[256]; ++ palette_entry image_palette[256]; + unsigned char id_length, palette_type, image_type, palette_entry_size; + unsigned char bpp, descriptor_bits; + short unsigned int palette_colors; ++ short unsigned int palette_start; + short unsigned int image_width, image_height; + bool left_to_right; + bool top_to_bottom; +@@ -299,7 +302,7 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + id_length = al_fgetc(f); + palette_type = al_fgetc(f); + image_type = al_fgetc(f); +- al_fread16le(f); /* first_color */ ++ palette_start = al_fread16le(f); /* first_color */ + palette_colors = al_fread16le(f); + palette_entry_size = al_fgetc(f); + al_fread16le(f); /* left */ +@@ -314,35 +317,6 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + + al_fread(f, image_id, id_length); + +- if (palette_type == 1) { +- +- for (i = 0; i < palette_colors; i++) { +- +- switch (palette_entry_size) { +- +- case 16: +- c = al_fread16le(f); +- image_palette[i][0] = (c & 0x1F) << 3; +- image_palette[i][1] = ((c >> 5) & 0x1F) << 3; +- image_palette[i][2] = ((c >> 10) & 0x1F) << 3; +- break; +- +- case 24: +- case 32: +- image_palette[i][0] = al_fgetc(f); +- image_palette[i][1] = al_fgetc(f); +- image_palette[i][2] = al_fgetc(f); +- if (palette_entry_size == 32) +- al_fgetc(f); +- break; +- } +- } +- } +- else if (palette_type != 0) { +- ALLEGRO_ERROR("Invalid palette type %d.\n", palette_type); +- return NULL; +- } +- + /* Image type: + * 0 = no image data + * 1 = uncompressed color mapped +@@ -364,7 +338,9 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + + case 1: + /* paletted image */ +- if ((palette_type != 1) || (bpp != 8)) { ++ /* Only support 8 bit palettes (up to 256 entries) though in principle the file format could have more(?)*/ ++ if ((palette_type != 1) || (bpp != 8) || (palette_start + palette_colors) > 256) { ++ ALLEGRO_ERROR("Invalid palette/image/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); + return NULL; + } + +@@ -402,6 +378,38 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + return NULL; + } + ++ if (palette_type == 0) { ++ /* No-op */ ++ } else if (palette_type == 1) { ++ for (i = 0; i < palette_colors; i++) { ++ palette_entry *entry = image_palette + palette_start + i; ++ switch (palette_entry_size) { ++ case 16: ++ c = al_fread16le(f); ++ (*entry)[0] = (c & 0x1F) << 3; ++ (*entry)[1] = ((c >> 5) & 0x1F) << 3; ++ (*entry)[2] = ((c >> 10) & 0x1F) << 3; ++ break; ++ case 24: ++ (*entry)[0] = al_fgetc(f); ++ (*entry)[1] = al_fgetc(f); ++ (*entry)[2] = al_fgetc(f); ++ break; ++ case 32: ++ (*entry)[0] = al_fgetc(f); ++ (*entry)[1] = al_fgetc(f); ++ (*entry)[2] = al_fgetc(f); ++ al_fgetc(f); /* Ignore 4th byte (alpha) */ ++ break; ++ } ++ } ++ } ++ else { ++ ALLEGRO_ERROR("Invalid palette type %d.\n", palette_type); ++ return NULL; ++ } ++ ++ + bmp = al_create_bitmap(image_width, image_height); + if (!bmp) { + ALLEGRO_ERROR("Failed to create bitmap.\n"); +@@ -452,9 +460,17 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + + unsigned char *dest = (unsigned char *)lr->data + + lr->pitch*true_y + true_x*4; +- dest[0] = image_palette[pix][2]; +- dest[1] = image_palette[pix][1]; +- dest[2] = image_palette[pix][0]; ++ if (pix < palette_start || pix >= (palette_start + palette_colors)) { ++ al_free(buf); ++ al_unlock_bitmap(bmp); ++ al_destroy_bitmap(bmp); ++ ALLEGRO_ERROR("Invalid image data.\n"); ++ return NULL; ++ } ++ palette_entry* entry = image_palette + pix; ++ dest[0] = (*entry)[2]; ++ dest[1] = (*entry)[1]; ++ dest[2] = (*entry)[0]; + dest[3] = 255; + } + diff -Nru allegro5-5.2.6.0/debian/patches/0009-Windows-BMP-Make-more-robust-to-crashing.patch allegro5-5.2.6.0/debian/patches/0009-Windows-BMP-Make-more-robust-to-crashing.patch --- allegro5-5.2.6.0/debian/patches/0009-Windows-BMP-Make-more-robust-to-crashing.patch 1970-01-01 01:00:00.000000000 +0100 +++ allegro5-5.2.6.0/debian/patches/0009-Windows-BMP-Make-more-robust-to-crashing.patch 2024-03-20 21:33:02.000000000 +0100 @@ -0,0 +1,461 @@ +From: Peter Hull +Date: Mon, 14 Jun 2021 21:47:17 +0100 +Applied-Upstream: https://github.com/liballeg/allegro5/commit/a2c93939f6997a96ecac1865dbb4fa3f66b5e1b7 +Subject: Windows BMP - Make more robust to crashing + +Validate compression mode and palette size + +Handle zero-sized bitmaps + +Fix text description to match the order of the fields + +Make sure RLE4/RLE8 decoders don't step outside their buffers +Track failure from the decoders and use to avoid leaks +Allow extra space to prevent direct color decoder reading outside its buffer +--- + addons/image/bmp.c | 179 ++++++++++++++++++++++++++++++++--------------------- + addons/image/tga.c | 6 +- + src/bitmap.c | 2 +- + 3 files changed, 112 insertions(+), 75 deletions(-) + +diff --git a/addons/image/bmp.c b/addons/image/bmp.c +index ab9d913..c1e0488 100644 +--- a/addons/image/bmp.c ++++ b/addons/image/bmp.c +@@ -756,11 +756,11 @@ static bool read_bitfields_image(ALLEGRO_FILE *f, int flags, + + height = infoheader->biHeight; + width = infoheader->biWidth; +- + // Includes enough space to read the padding for a line + linesize = width * bytes_per_pixel + ((width * bytes_per_pixel) & 3); + +- linebuf = al_malloc(linesize); ++ // Overallocate by bytes_per_pixel so a 32-bit read can overlap the end ++ linebuf = al_malloc((width + 1) * bytes_per_pixel); + + if (!linebuf) { + ALLEGRO_WARN("Failed to allocate pixel row buffer\n"); +@@ -954,16 +954,16 @@ static bool read_RGB_image_32bit_alpha_hack(ALLEGRO_FILE *f, int flags, + /* read_RLE8_compressed_image: + * For reading the 8 bit RLE compressed BMP image format. + */ +-static void read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, ++static bool read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + const BMPINFOHEADER *infoheader) + { + int count; + unsigned char val; +- unsigned char val0; +- int j, pos, line, height, dir; ++ int j, pos, line, width, height, dir; + int eolflag, eopicflag; + + eopicflag = 0; ++ width = (int)infoheader->biWidth; + height = abs((int)infoheader->biHeight); + line = (infoheader->biHeight < 0) ? 0 : height - 1; + dir = (infoheader->biHeight < 0) ? 1 : -1; +@@ -974,18 +974,18 @@ static void read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + + while ((eolflag == 0) && (eopicflag == 0)) { + count = al_fgetc(f); +- if (count == EOF) +- return; +- if (pos + count > (int)infoheader->biWidth) { +- ALLEGRO_WARN("overlong compressed line\n"); +- count = infoheader->biWidth - pos; ++ if (count == EOF) { ++ return false; + } +- + val = al_fgetc(f); + + if (count > 0) { /* repeat pixel count times */ ++ if (count > width - pos) { ++ count = width - pos; ++ } ++ + for (j = 0; j < count; j++) { +- buf[line * infoheader->biWidth + pos] = val; ++ buf[line * width + pos] = val; + pos++; + } + } +@@ -1002,28 +1002,40 @@ static void read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + + case 2: /* displace picture */ + count = al_fgetc(f); +- if (count == EOF) +- return; +- val = al_fgetc(f); ++ if (count == EOF) { ++ return false; ++ } + pos += count; +- line += dir * val; ++ count = al_fgetc(f); ++ if (count == EOF) { ++ return false; ++ } ++ line += dir * count; ++ if (line < 0) ++ line = 0; ++ if (line >= height) ++ line = height - 1; + break; + + default: /* read in absolute mode */ +- for (j=0; jbiWidth + pos] = val0; ++ count = val; ++ if (count > width - pos) { ++ count = width - pos; ++ } ++ for (j = 0; j < count; j++) { ++ val = al_fgetc(f); ++ buf[line * width + pos] = val; + pos++; + } + + if (j % 2 == 1) +- val0 = al_fgetc(f); /* align on word boundary */ ++ val = al_fgetc(f); /* align on word boundary */ + + break; + } + } + +- if (pos - 1 > (int)infoheader->biWidth) ++ if (pos > width + 1) + eolflag = 1; + } + +@@ -1031,6 +1043,7 @@ static void read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + if (line < 0 || line >= height) + eopicflag = 1; + } ++ return true; + } + + +@@ -1038,16 +1051,17 @@ static void read_RLE8_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + /* read_RLE4_compressed_image: + * For reading the 4 bit RLE compressed BMP image format. + */ +-static void read_RLE4_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, ++static bool read_RLE4_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + const BMPINFOHEADER *infoheader) + { + unsigned char b[8]; + int count; +- unsigned short val0, val; +- int j, k, pos, line, height, dir; ++ unsigned short val; ++ int j, k, pos, line, width, height, dir; + int eolflag, eopicflag; + + eopicflag = 0; /* end of picture flag */ ++ width = (int)infoheader->biWidth; + height = abs((int)infoheader->biHeight); + line = (infoheader->biHeight < 0) ? 0 : height - 1; + dir = (infoheader->biHeight < 0) ? 1 : -1; +@@ -1059,19 +1073,18 @@ static void read_RLE4_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + while ((eolflag == 0) && (eopicflag == 0)) { + count = al_fgetc(f); + if (count == EOF) +- return; +- if (pos + count > (int)infoheader->biWidth) { +- ALLEGRO_WARN("overlong compressed line\n"); +- count = infoheader->biWidth - pos; +- } ++ return false; + + val = al_fgetc(f); + + if (count > 0) { /* repeat pixels count times */ ++ if (count > width - pos) { ++ count = width - pos; ++ } + b[1] = val & 15; + b[0] = (val >> 4) & 15; + for (j = 0; j < count; j++) { +- buf[line * infoheader->biWidth + pos] = b[j % 2]; ++ buf[line * width + pos] = b[j % 2]; + pos++; + } + } +@@ -1089,31 +1102,41 @@ static void read_RLE4_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + case 2: /* displace image */ + count = al_fgetc(f); + if (count == EOF) +- return; +- val = al_fgetc(f); ++ return false; + pos += count; +- line += dir * val; ++ count = al_fgetc(f); ++ if (count == EOF) ++ return false; ++ line += dir * count; ++ if (line < 0) ++ line = 0; ++ if (line >= height) ++ line = height - 1; + break; + + default: /* read in absolute mode */ +- for (j = 0; j < val; j++) { ++ count = val; ++ if (count > width - pos) { ++ count = width - pos; ++ } ++ for (j = 0; j < count; j++) { + if ((j % 4) == 0) { +- val0 = (uint16_t)al_fread16le(f); ++ val = (uint16_t)al_fread16le(f); + for (k = 0; k < 2; k++) { +- b[2 * k + 1] = val0 & 15; +- val0 = val0 >> 4; +- b[2 * k] = val0 & 15; +- val0 = val0 >> 4; ++ b[2 * k + 1] = val & 15; ++ val = val >> 4; ++ b[2 * k] = val & 15; ++ val = val >> 4; + } + } +- buf[line * infoheader->biWidth + pos] = b[j % 4]; ++ buf[line * width + pos] = b[j % 4]; + pos++; + } + break; + } + } + +- if (pos - 1 > (int)infoheader->biWidth) ++ if (pos > width + 1) + eolflag = 1; + } + +@@ -1121,6 +1144,7 @@ static void read_RLE4_compressed_image(ALLEGRO_FILE *f, unsigned char *buf, + if (line < 0 || line >= height) + eopicflag = 1; + } ++ return true; + } + + +@@ -1143,6 +1167,7 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + unsigned char *buf = NULL; + ALLEGRO_LOCKED_REGION *lr; + bool keep_index = INT_TO_BOOL(flags & ALLEGRO_KEEP_INDEX); ++ bool loaded_ok; + + ASSERT(f); + +@@ -1192,7 +1217,13 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + ALLEGRO_WARN("negative width: %ld\n", infoheader.biWidth); + return NULL; + } +- ++ if (infoheader.biCompression != BIT_RGB ++ && infoheader.biCompression != BIT_RLE8 ++ && infoheader.biCompression != BIT_RLE4 ++ && infoheader.biCompression != BIT_BITFIELDS) { ++ ALLEGRO_ERROR("Unsupported compression: 0x%x\n", (int) infoheader.biCompression); ++ return NULL; ++ } + if (infoheader.biBitCount != 1 && infoheader.biBitCount != 2 && infoheader.biBitCount != 4 && + infoheader.biBitCount != 8 && infoheader.biBitCount != 16 && infoheader.biBitCount != 24 && + infoheader.biBitCount != 32) { +@@ -1274,6 +1305,11 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + int extracolors = 0; + int bytes_per_color = win_flag ? 4 : 3; + ++ if (infoheader.biClrUsed >= INT_MAX) { ++ ALLEGRO_ERROR("Illegal palette size: %lu\n", infoheader.biClrUsed); ++ return NULL; ++ } ++ + if (win_flag) { + if (ncolors == 0) { + ncolors = (1 << infoheader.biBitCount); +@@ -1330,7 +1366,12 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + ALLEGRO_ERROR("Failed to create bitmap\n"); + return NULL; + } +- ++ ++ if (infoheader.biWidth == 0 || infoheader.biHeight == 0) { ++ ALLEGRO_WARN("Creating zero-sized bitmap\n"); ++ return bmp; ++ } ++ + if (infoheader.biBitCount <= 8 && keep_index) { + lr = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8, + ALLEGRO_LOCK_WRITEONLY); +@@ -1356,7 +1397,7 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + /* RLE decoding may skip pixels so clear the buffer first. */ + buf = al_calloc(infoheader.biWidth, abs((int)infoheader.biHeight)); + } +- ++ loaded_ok = true; + switch (infoheader.biCompression) { + case BIT_RGB: + if (infoheader.biBitCount == 32 && !infoheader.biHaveAlphaMask) { +@@ -1396,79 +1437,70 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + break; + + case BIT_RLE8: +- read_RLE8_compressed_image(f, buf, &infoheader); ++ loaded_ok = read_RLE8_compressed_image(f, buf, &infoheader); ++ if (!loaded_ok) ++ ALLEGRO_ERROR("Error reading RLE8 data\n"); + break; + + case BIT_RLE4: +- read_RLE4_compressed_image(f, buf, &infoheader); ++ loaded_ok = read_RLE4_compressed_image(f, buf, &infoheader); ++ if (!loaded_ok) ++ ALLEGRO_ERROR("Error reading RLE4 data\n"); + break; + + case BIT_BITFIELDS: + if (infoheader.biBitCount == 16) { + if (infoheader.biRedMask == 0x00007C00U && infoheader.biGreenMask == 0x000003E0U && + infoheader.biBlueMask == 0x0000001FU && infoheader.biAlphaMask == 0x00000000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_16_rgb_555_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_16_rgb_555_line); + } + else if (infoheader.biRedMask == 0x00007C00U && infoheader.biGreenMask == 0x000003E0U && + infoheader.biBlueMask == 0x0000001FU && infoheader.biAlphaMask == 0x00008000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_16_argb_1555_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_16_argb_1555_line); + } + else if (infoheader.biRedMask == 0x0000F800U && infoheader.biGreenMask == 0x000007E0U && + infoheader.biBlueMask == 0x0000001FU && infoheader.biAlphaMask == 0x00000000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_16_rgb_565_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_16_rgb_565_line); + } + else { +- if (!read_bitfields_image(f, flags, &infoheader, lr)) +- return NULL; ++ loaded_ok = read_bitfields_image(f, flags, &infoheader, lr); + } + } + else if (infoheader.biBitCount == 24) { + if (infoheader.biRedMask == 0x00FF0000U && infoheader.biGreenMask == 0x0000FF00U && + infoheader.biBlueMask == 0x000000FFU && infoheader.biAlphaMask == 0x00000000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_24_rgb_888_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_24_rgb_888_line); + } + else { +- if (!read_bitfields_image(f, flags, &infoheader, lr)) +- return NULL; ++ loaded_ok = read_bitfields_image(f, flags, &infoheader, lr); + } + } + else if (infoheader.biBitCount == 32) { + if (infoheader.biRedMask == 0x00FF0000U && infoheader.biGreenMask == 0x0000FF00U && + infoheader.biBlueMask == 0x000000FFU && infoheader.biAlphaMask == 0x00000000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_32_xrgb_8888_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_xrgb_8888_line); + } + else if (infoheader.biRedMask == 0x00FF0000U && infoheader.biGreenMask == 0x0000FF00U && + infoheader.biBlueMask == 0x000000FFU && infoheader.biAlphaMask == 0xFF000000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_32_argb_8888_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_argb_8888_line); + } + else if (infoheader.biRedMask == 0xFF000000U && infoheader.biGreenMask == 0x00FF0000U && + infoheader.biBlueMask == 0x0000FF00U && infoheader.biAlphaMask == 0x00000000U) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_32_rgbx_8888_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_rgbx_8888_line); + } + else if (infoheader.biRedMask == 0xFF000000U && infoheader.biGreenMask == 0x00FF0000U && + infoheader.biBlueMask == 0x0000FF00U && infoheader.biAlphaMask == 0x000000FFU) { +- if (!read_RGB_image(f, flags, &infoheader, lr, read_32_rgba_8888_line)) +- return NULL; ++ loaded_ok = read_RGB_image(f, flags, &infoheader, lr, read_32_rgba_8888_line); + } + else { +- if (!read_bitfields_image(f, flags, &infoheader, lr)) +- return NULL; ++ loaded_ok = read_bitfields_image(f, flags, &infoheader, lr); + } + } + break; + + default: + ALLEGRO_WARN("Unknown compression: %ld\n", infoheader.biCompression); +- al_unlock_bitmap(bmp); +- al_destroy_bitmap(bmp); +- bmp = NULL; ++ loaded_ok = false; + break; + } + +@@ -1499,6 +1531,11 @@ ALLEGRO_BITMAP *_al_load_bmp_f(ALLEGRO_FILE *f, int flags) + if (bmp) { + al_unlock_bitmap(bmp); + } ++ /* If something went wrong internally */ ++ if (!loaded_ok) { ++ al_destroy_bitmap(bmp); ++ bmp = NULL; ++ } + + return bmp; + } +diff --git a/addons/image/tga.c b/addons/image/tga.c +index 374f8de..17b56f4 100644 +--- a/addons/image/tga.c ++++ b/addons/image/tga.c +@@ -340,7 +340,7 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + /* paletted image */ + /* Only support 8 bit palettes (up to 256 entries) though in principle the file format could have more(?)*/ + if ((palette_type != 1) || (bpp != 8) || (palette_start + palette_colors) > 256) { +- ALLEGRO_ERROR("Invalid palette/image/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); ++ ALLEGRO_ERROR("Invalid image/palette/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); + return NULL; + } + +@@ -354,7 +354,7 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + else if ((palette_type == 0) && ((bpp == 24) || (bpp == 32))) { + } + else { +- ALLEGRO_ERROR("Invalid palette/image/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); ++ ALLEGRO_ERROR("Invalid image/palette/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); + return NULL; + } + break; +@@ -362,7 +362,7 @@ ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags) + case 3: + /* grayscale image */ + if ((palette_type != 0) || (bpp != 8)) { +- ALLEGRO_ERROR("Invalid palette/image/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); ++ ALLEGRO_ERROR("Invalid image/palette/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp); + return NULL; + } + +diff --git a/src/bitmap.c b/src/bitmap.c +index b553c9e..e8a4f1a 100644 +--- a/src/bitmap.c ++++ b/src/bitmap.c +@@ -106,7 +106,7 @@ ALLEGRO_BITMAP *_al_create_bitmap_params(ALLEGRO_DISPLAY *current_display, + * int. Supporting such bitmaps would require a lot more work. + * Overflow calc based on https://stackoverflow.com/a/1514309/231929 + */ +- if (w < 0 || h < 0 || (int64_t) w > (INT_MAX/4) / (int64_t) h) { ++ if (w < 0 || h < 0 || (h > 0 && (int64_t) w > (INT_MAX/4) / (int64_t) h)) { + ALLEGRO_WARN("Rejecting %dx%d bitmap\n", w, h); + return NULL; + } diff -Nru allegro5-5.2.6.0/debian/patches/series allegro5-5.2.6.0/debian/patches/series --- allegro5-5.2.6.0/debian/patches/series 2020-11-27 01:07:24.000000000 +0100 +++ allegro5-5.2.6.0/debian/patches/series 2023-04-10 14:58:44.000000000 +0200 @@ -3,3 +3,7 @@ use-debians-dejavu-font.patch 0004-Migrate-GTK2-to-GTK3.patch 0005-Fix-warnings-in-make_doc.patch +0006-Prevent-RLE-data-in-corrupt-.tga-from-overflowing-th.patch +0007-Reject-creating-bitmaps-with-negative-width-or-heigh.patch +0008-PCX-loader-validation.patch +0009-Windows-BMP-Make-more-robust-to-crashing.patch