1 From e623866d9286410156e8b9d2c82d6253a1b22d08 Mon Sep 17 00:00:00 2001
2 From: Daniel Axtens <dja@axtens.net>
3 Date: Tue, 6 Jul 2021 18:51:35 +1000
4 Subject: [PATCH] video/readers/png: Drop greyscale support to fix heap
7 A 16-bit greyscale PNG without alpha is processed in the following loop:
9 for (i = 0; i < (data->image_width * data->image_height);
10 i++, d1 += 4, d2 += 2)
17 The increment of d1 is wrong. d1 is incremented by 4 bytes per iteration,
18 but there are only 3 bytes allocated for storage. This means that image
19 data will overwrite somewhat-attacker-controlled parts of memory - 3 bytes
20 out of every 4 following the end of the image.
22 This has existed since greyscale support was added in 2013 in commit
23 3ccf16dff98f (grub-core/video/readers/png.c: Support grayscale).
25 Saving starfield.png as a 16-bit greyscale image without alpha in the gimp
26 and attempting to load it causes grub-emu to crash - I don't think this code
29 Delete all PNG greyscale support.
33 Signed-off-by: Daniel Axtens <dja@axtens.net>
34 Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
36 Upstream-Status: Backport
39 Reference to upstream patch:
40 https://git.savannah.gnu.org/cgit/grub.git/commit/?id=e623866d9286410156e8b9d2c82d6253a1b22d08
42 Signed-off-by: Yongxin Liu <yongxin.liu@windriver.com>
44 grub-core/video/readers/png.c | 87 +++--------------------------------
45 1 file changed, 7 insertions(+), 80 deletions(-)
47 diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c
48 index 35ae553c8..a3161e25b 100644
49 --- a/grub-core/video/readers/png.c
50 +++ b/grub-core/video/readers/png.c
51 @@ -100,7 +100,7 @@ struct grub_png_data
53 unsigned image_width, image_height;
55 - int raw_bytes, is_gray, is_alpha, is_palette;
56 + int raw_bytes, is_alpha, is_palette;
57 int row_bytes, color_bits;
58 grub_uint8_t *image_data;
60 @@ -296,13 +296,13 @@ grub_png_decode_image_header (struct grub_png_data *data)
66 + return grub_error (GRUB_ERR_BAD_FILE_TYPE,
67 + "png: color type not supported");
70 if ((color_bits != 8) && (color_bits != 16)
72 - || !(data->is_gray || data->is_palette)))
73 + || !data->is_palette))
74 return grub_error (GRUB_ERR_BAD_FILE_TYPE,
75 "png: bit depth must be 8 or 16");
77 @@ -331,7 +331,7 @@ grub_png_decode_image_header (struct grub_png_data *data)
80 #ifndef GRUB_CPU_WORDS_BIGENDIAN
81 - if (data->is_16bit || data->is_gray || data->is_palette)
82 + if (data->is_16bit || data->is_palette)
85 data->image_data = grub_calloc (data->image_height, data->row_bytes);
86 @@ -899,27 +899,8 @@ grub_png_convert_image (struct grub_png_data *data)
88 int mask = (1 << data->color_bits) - 1;
92 - /* Generic formula is
93 - (0xff * i) / ((1U << data->color_bits) - 1)
94 - but for allowed bit depth of 1, 2 and for it's
96 - (0xff / ((1U << data->color_bits) - 1)) * i
97 - Precompute the multipliers to avoid division.
100 - const grub_uint8_t multipliers[5] = { 0xff, 0xff, 0x55, 0x24, 0x11 };
101 - for (i = 0; i < (1U << data->color_bits); i++)
103 - grub_uint8_t col = multipliers[data->color_bits] * i;
104 - palette[i][0] = col;
105 - palette[i][1] = col;
106 - palette[i][2] = col;
110 - grub_memcpy (palette, data->palette, 3 << data->color_bits);
112 + grub_memcpy (palette, data->palette, 3 << data->color_bits);
115 for (j = 0; j < data->image_height; j++, d1c += data->image_width * 3,
116 @@ -957,60 +938,6 @@ grub_png_convert_image (struct grub_png_data *data)
125 - /* 16-bit gray with alpha. */
126 - for (i = 0; i < (data->image_width * data->image_height);
127 - i++, d1 += 4, d2 += 4)
136 - if (data->is_16bit)
137 - /* 16-bit gray without alpha. */
139 - for (i = 0; i < (data->image_width * data->image_height);
140 - i++, d1 += 4, d2 += 2)
148 - /* 8-bit gray with alpha. */
150 - for (i = 0; i < (data->image_width * data->image_height);
151 - i++, d1 += 4, d2 += 2)
160 - /* 8-bit gray without alpha. */
162 - for (i = 0; i < (data->image_width * data->image_height);
163 - i++, d1 += 3, d2++)
175 /* Only copy the upper 8 bit. */
176 #ifndef GRUB_CPU_WORDS_BIGENDIAN