Skip to content

Commit 89bd46d

Browse files
committed
stop JPEG load after 20 warnings
mitigates some DoS attacks somewhat see https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24383
1 parent 55b857d commit 89bd46d

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

libvips/foreign/jpeg2vips.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ read_jpeg_header( ReadJpeg *jpeg, VipsImage *out )
488488
size_t data_length;
489489
int i;
490490

491+
/* Trace level 3 means emit warning messages as they happen. This
492+
* lets us spot files with crazy numbers of warnings early and
493+
* prevents some DoS attacks.
494+
*/
495+
jpeg->eman.pub.trace_level = 3;
496+
491497
/* Read JPEG header. libjpeg will set out_color_space sanely for us
492498
* for YUV YCCK etc.
493499
*/

libvips/foreign/vips2jpeg.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,34 @@
165165
void
166166
vips__new_output_message( j_common_ptr cinfo )
167167
{
168-
char buffer[JMSG_LENGTH_MAX];
168+
/* Some DoS attacks use jpg files with thousands of warnings. Try to
169+
* limit the effect these have.
170+
*/
171+
if( cinfo->err->num_warnings >= 20 ) {
172+
if( cinfo->err->num_warnings == 20 ) {
173+
vips_error( "VipsJpeg",
174+
"%s", _( "too many warnings" ) );
175+
}
176+
177+
jpeg_abort( cinfo );
178+
}
179+
else {
180+
char buffer[JMSG_LENGTH_MAX];
169181

170-
(*cinfo->err->format_message)( cinfo, buffer );
171-
vips_error( "VipsJpeg", _( "%s" ), buffer );
182+
(*cinfo->err->format_message)( cinfo, buffer );
183+
vips_error( "VipsJpeg", _( "%s" ), buffer );
172184

173185
#ifdef DEBUG
174-
printf( "vips__new_output_message: \"%s\"\n", buffer );
186+
printf( "vips__new_output_message: \"%s\"\n", buffer );
175187
#endif /*DEBUG*/
176188

177-
/* This is run for things like file truncated. Signal invalidate to
178-
* force this op out of cache.
179-
*/
180-
if( cinfo->client_data )
181-
vips_foreign_load_invalidate( VIPS_IMAGE( cinfo->client_data ) );
189+
/* This is run for things like file truncated. Signal
190+
* invalidate to force this op out of cache.
191+
*/
192+
if( cinfo->client_data )
193+
vips_foreign_load_invalidate(
194+
VIPS_IMAGE( cinfo->client_data ) );
195+
}
182196
}
183197

184198
/* New error_exit handler.

0 commit comments

Comments
 (0)