Blame SOURCES/SDL-1.2.15-CVE-2019-7575-Fix-a-buffer-overwrite-in-MS_ADPCM_dec.patch

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