Skip to content

Commit 68e090e

Browse files
jbowlerctruta
authored andcommitted
fix: png_write_iCCP check on profile length
This is a regression of commit a8242dd "PNGv3 colourspace precedence rules conformance". Previously, `png_write_iCCP` used the length from the first four bytes of the profile set by `png_set_iCCP`, rather than the actual data length recorded by `png_set_iCCP`. If the profile data were less than 4 bytes long, it would have caused a read-beyond-end-of-malloc error. This bug was in the libpng code even before the changes introduced in the above-mentioned commit, but it was inaccessible. It became accessible when we removed the pre-PNGv3 colour space checks in `png_set_iCCP`. Reported-by: Bob Friesenhahn <bobjfriesenhahn@gmail.com> Reviewed-by: Cosmin Truta <ctruta@gmail.com> Signed-off-by: John Bowler <jbowler@acm.org> Signed-off-by: Cosmin Truta <ctruta@gmail.com>
1 parent 8c7ed2e commit 68e090e

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

pngpriv.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,10 +1276,10 @@ PNG_INTERNAL_FUNCTION(void,png_write_eXIf,(png_structrp png_ptr,
12761276

12771277
#ifdef PNG_WRITE_iCCP_SUPPORTED
12781278
PNG_INTERNAL_FUNCTION(void,png_write_iCCP,(png_structrp png_ptr,
1279-
png_const_charp name, png_const_bytep profile), PNG_EMPTY);
1280-
/* The profile must have been previously validated for correctness, the
1281-
* length comes from the first four bytes. Only the base, deflate,
1282-
* compression is supported.
1279+
png_const_charp name, png_const_bytep profile, png_uint_32 proflen),
1280+
PNG_EMPTY);
1281+
/* Writes a previously 'set' profile. The profile argument is **not**
1282+
* compressed.
12831283
*/
12841284
#endif
12851285

pngwrite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
196196
if ((info_ptr->valid & PNG_INFO_iCCP) != 0)
197197
{
198198
png_write_iCCP(png_ptr, info_ptr->iccp_name,
199-
info_ptr->iccp_profile);
199+
info_ptr->iccp_profile, info_ptr->iccp_proflen);
200200
}
201201
# endif
202202

pngwutil.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,10 +1132,9 @@ png_write_sRGB(png_structrp png_ptr, int srgb_intent)
11321132
/* Write an iCCP chunk */
11331133
void /* PRIVATE */
11341134
png_write_iCCP(png_structrp png_ptr, png_const_charp name,
1135-
png_const_bytep profile)
1135+
png_const_bytep profile, png_uint_32 profile_len)
11361136
{
11371137
png_uint_32 name_len;
1138-
png_uint_32 profile_len;
11391138
png_byte new_name[81]; /* 1 byte for the compression byte */
11401139
compression_state comp;
11411140
png_uint_32 temp;
@@ -1148,11 +1147,12 @@ png_write_iCCP(png_structrp png_ptr, png_const_charp name,
11481147
if (profile == NULL)
11491148
png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */
11501149

1151-
profile_len = png_get_uint_32(profile);
1152-
11531150
if (profile_len < 132)
11541151
png_error(png_ptr, "ICC profile too short");
11551152

1153+
if (png_get_uint_32(profile) != profile_len)
1154+
png_error(png_ptr, "Incorrect data in iCCP");
1155+
11561156
temp = (png_uint_32) (*(profile+8));
11571157
if (temp > 3 && (profile_len & 0x03))
11581158
png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)");

0 commit comments

Comments
 (0)