CVE-2019-7575: Fix a buffer overwrite in MS_ADPCM_decode SDL-1.2
authorPetr Písař <ppisar@redhat.com>
Mon, 10 Jun 2019 09:25:05 -0700
branchSDL-1.2
changeset 12821a936f9bd3e38
parent 12819 fcbecae42795
child 12831 f1f5878be5db
CVE-2019-7575: Fix a buffer overwrite in MS_ADPCM_decode
If a WAV format defines shorter audio stream and decoded MS ADPCM data chunk
is longer, decoding continued past the output audio buffer.

This fix is based on a patch from
<https://bugzilla.libsdl.org/show_bug.cgi?id=4492>.

https://bugzilla.libsdl.org/show_bug.cgi?id=4493
CVE-2019-7575

Signed-off-by: Petr Písař <ppisar@redhat.com>
src/audio/SDL_wave.c
     1.1 --- a/src/audio/SDL_wave.c	Mon Jun 10 09:06:23 2019 -0700
     1.2 +++ b/src/audio/SDL_wave.c	Mon Jun 10 09:25:05 2019 -0700
     1.3 @@ -122,7 +122,7 @@
     1.4  static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
     1.5  {
     1.6  	struct MS_ADPCM_decodestate *state[2];
     1.7 -	Uint8 *freeable, *encoded, *encoded_end, *decoded;
     1.8 +	Uint8 *freeable, *encoded, *encoded_end, *decoded, *decoded_end;
     1.9  	Sint32 encoded_len, samplesleft;
    1.10  	Sint8 nybble, stereo;
    1.11  	Sint16 *coeff[2];
    1.12 @@ -142,6 +142,7 @@
    1.13  		return(-1);
    1.14  	}
    1.15  	decoded = *audio_buf;
    1.16 +	decoded_end = decoded + *audio_len;
    1.17  
    1.18  	/* Get ready... Go! */
    1.19  	stereo = (MS_ADPCM_state.wavefmt.channels == 2);
    1.20 @@ -149,7 +150,7 @@
    1.21  	state[1] = &MS_ADPCM_state.state[stereo];
    1.22  	while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) {
    1.23  		/* Grab the initial information for this block */
    1.24 -		if (encoded + 7 + (stereo ? 7 : 0) > encoded_end) goto too_short;
    1.25 +		if (encoded + 7 + (stereo ? 7 : 0) > encoded_end) goto invalid_size;
    1.26  		state[0]->hPredictor = *encoded++;
    1.27  		if ( stereo ) {
    1.28  			state[1]->hPredictor = *encoded++;
    1.29 @@ -179,6 +180,7 @@
    1.30  		coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
    1.31  
    1.32  		/* Store the two initial samples we start with */
    1.33 +		if (decoded + 4 + (stereo ? 4 : 0) > decoded_end) goto invalid_size;
    1.34  		decoded[0] = state[0]->iSamp2&0xFF;
    1.35  		decoded[1] = state[0]->iSamp2>>8;
    1.36  		decoded += 2;
    1.37 @@ -200,7 +202,8 @@
    1.38  		samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)*
    1.39  					MS_ADPCM_state.wavefmt.channels;
    1.40  		while ( samplesleft > 0 ) {
    1.41 -			if (encoded + 1 > encoded_end) goto too_short;
    1.42 +			if (encoded + 1 > encoded_end) goto invalid_size;
    1.43 +			if (decoded + 4 > decoded_end) goto invalid_size;
    1.44  
    1.45  			nybble = (*encoded)>>4;
    1.46  			new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]);
    1.47 @@ -223,8 +226,8 @@
    1.48  	}
    1.49  	SDL_free(freeable);
    1.50  	return(0);
    1.51 -too_short:
    1.52 -	SDL_SetError("Too short chunk for a MS ADPCM decoder");
    1.53 +invalid_size:
    1.54 +	SDL_SetError("Unexpected chunk length for a MS ADPCM decoder");
    1.55  	SDL_free(freeable);
    1.56  	return(-1);
    1.57  invalid_predictor: