src/flac.c: Fix a buffer read overflow 00/211500/1
authorErik de Castro Lopo <erikd@mega-nerd.com>
Fri, 14 Apr 2017 05:19:16 +0000 (15:19 +1000)
committerSeungbae Shin <seungbae.shin@samsung.com>
Tue, 6 Aug 2019 04:04:40 +0000 (13:04 +0900)
A file (generated by a fuzzer) which increased the number of channels
from one frame to the next could cause a read beyond the end of the
buffer provided by libFLAC. Only option is to abort the read.

Change-Id: Ibf8626f9a485c4b0a5db219d413651444f56e382
Closes: https://github.com/erikd/libsndfile/issues/231

src/flac.c

index 2992f4c..ddbb015 100644 (file)
@@ -169,6 +169,14 @@ flac_buffer_copy (SF_PRIVATE *psf)
        const int32_t* const *buffer = pflac->wbuffer ;
        unsigned i = 0, j, offset, channels, len ;
 
+       if (psf->sf.channels != (int) frame->header.channels)
+       {       psf_log_printf (psf, "Error: FLAC frame changed from %d to %d channels\n"
+                                                                       "Nothing to do but to error out.\n" ,
+                                                                       psf->sf.channels, frame->header.channels) ;
+               psf->error = SFE_FLAC_CHANNEL_COUNT_CHANGED ;
+               return 0 ;
+               } ;
+
        /*
        **      frame->header.blocksize is variable and we're using a constant blocksize
        **      of FLAC__MAX_BLOCK_SIZE.
@@ -202,7 +210,6 @@ flac_buffer_copy (SF_PRIVATE *psf)
                return 0 ;
                } ;
 
-
        len = SF_MIN (pflac->len, frame->header.blocksize) ;
 
        if (pflac->remain % channels != 0)
@@ -436,7 +443,7 @@ sf_flac_meta_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC_
        {       case FLAC__METADATA_TYPE_STREAMINFO :
                        if (psf->sf.channels > 0 && psf->sf.channels != (int) metadata->data.stream_info.channels)
                        {       psf_log_printf (psf, "Error: FLAC stream changed from %d to %d channels\n"
-                                                                       "Nothing to be but to error out.\n" ,
+                                                                       "Nothing to do but to error out.\n" ,
                                                                        psf->sf.channels, metadata->data.stream_info.channels) ;
                                psf->error = SFE_FLAC_CHANNEL_COUNT_CHANGED ;
                                return ;