0d0d62e
From e1f80cadb079e35103e6eebf160a818815c823df Mon Sep 17 00:00:00 2001
0d0d62e
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
0d0d62e
Date: Thu, 14 Feb 2019 14:51:52 +0100
0d0d62e
Subject: [PATCH] CVE-2019-7575: Fix a buffer overwrite in MS_ADPCM_decode
0d0d62e
MIME-Version: 1.0
0d0d62e
Content-Type: text/plain; charset=UTF-8
0d0d62e
Content-Transfer-Encoding: 8bit
0d0d62e
0d0d62e
If a WAV format defines shorter audio stream and decoded MS ADPCM data chunk
0d0d62e
is longer, decoding continued past the output audio buffer.
0d0d62e
0d0d62e
This fix is based on a patch from
0d0d62e
<https://bugzilla.libsdl.org/show_bug.cgi?id=4492>.
0d0d62e
0d0d62e
https://bugzilla.libsdl.org/show_bug.cgi?id=4493
0d0d62e
CVE-2019-7575
0d0d62e
0d0d62e
Signed-off-by: Petr Písař <ppisar@redhat.com>
0d0d62e
---
0d0d62e
 src/audio/SDL_wave.c | 13 ++++++++-----
0d0d62e
 1 file changed, 8 insertions(+), 5 deletions(-)
0d0d62e
0d0d62e
diff --git a/src/audio/SDL_wave.c b/src/audio/SDL_wave.c
0d0d62e
index e42d01c..b6c49de 100644
0d0d62e
--- a/src/audio/SDL_wave.c
0d0d62e
+++ b/src/audio/SDL_wave.c
0d0d62e
@@ -115,7 +115,7 @@ static Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
0d0d62e
 static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
0d0d62e
 {
0d0d62e
 	struct MS_ADPCM_decodestate *state[2];
0d0d62e
-	Uint8 *freeable, *encoded, *encoded_end, *decoded;
0d0d62e
+	Uint8 *freeable, *encoded, *encoded_end, *decoded, *decoded_end;
0d0d62e
 	Sint32 encoded_len, samplesleft;
0d0d62e
 	Sint8 nybble, stereo;
0d0d62e
 	Sint16 *coeff[2];
0d0d62e
@@ -135,6 +135,7 @@ static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
0d0d62e
 		return(-1);
0d0d62e
 	}
0d0d62e
 	decoded = *audio_buf;
0d0d62e
+	decoded_end = decoded + *audio_len;
0d0d62e
 
0d0d62e
 	/* Get ready... Go! */
0d0d62e
 	stereo = (MS_ADPCM_state.wavefmt.channels == 2);
0d0d62e
@@ -142,7 +143,7 @@ static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
0d0d62e
 	state[1] = &MS_ADPCM_state.state[stereo];
0d0d62e
 	while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) {
0d0d62e
 		/* Grab the initial information for this block */
0d0d62e
-		if (encoded + 7 + (stereo ? 7 : 0) > encoded_end) goto too_short;
0d0d62e
+		if (encoded + 7 + (stereo ? 7 : 0) > encoded_end) goto invalid_size;
0d0d62e
 		state[0]->hPredictor = *encoded++;
0d0d62e
 		if ( stereo ) {
0d0d62e
 			state[1]->hPredictor = *encoded++;
0d0d62e
@@ -169,6 +170,7 @@ static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
0d0d62e
 		coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
0d0d62e
 
0d0d62e
 		/* Store the two initial samples we start with */
0d0d62e
+		if (decoded + 4 + (stereo ? 4 : 0) > decoded_end) goto invalid_size;
0d0d62e
 		decoded[0] = state[0]->iSamp2&0xFF;
0d0d62e
 		decoded[1] = state[0]->iSamp2>>8;
0d0d62e
 		decoded += 2;
0d0d62e
@@ -190,7 +192,8 @@ static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
0d0d62e
 		samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)*
0d0d62e
 					MS_ADPCM_state.wavefmt.channels;
0d0d62e
 		while ( samplesleft > 0 ) {
0d0d62e
-			if (encoded + 1 > encoded_end) goto too_short;
0d0d62e
+			if (encoded + 1 > encoded_end) goto invalid_size;
0d0d62e
+			if (decoded + 4 > decoded_end) goto invalid_size;
0d0d62e
 
0d0d62e
 			nybble = (*encoded)>>4;
0d0d62e
 			new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]);
0d0d62e
@@ -213,8 +216,8 @@ static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
0d0d62e
 	}
0d0d62e
 	SDL_free(freeable);
0d0d62e
 	return(0);
0d0d62e
-too_short:
0d0d62e
-	SDL_SetError("Too short chunk for a MS ADPCM decoder");
0d0d62e
+invalid_size:
0d0d62e
+	SDL_SetError("Unexpected chunk length for a MS ADPCM decoder");
0d0d62e
 	SDL_free(freeable);
0d0d62e
 	return(-1);
0d0d62e
 }
0d0d62e
-- 
0d0d62e
2.20.1
0d0d62e