File freerdp-CVE-2026-31883_31885.patch of Package freerdp.43418

From 16df2300e1e3f5a51f68fb1626429e58b531b7c8 Mon Sep 17 00:00:00 2001
From: Armin Novak <armin.novak@thincast.com>
Date: Tue, 10 Mar 2026 10:21:40 +0100
Subject: [PATCH] [codec,dsp] fix array bounds checks

* assert array indices where caller value is an internal constant
* add missing length/bounds checks
---
 libfreerdp/codec/dsp.c | 79 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 70 insertions(+), 9 deletions(-)

Index: FreeRDP-2.4.0/libfreerdp/codec/dsp.c
===================================================================
--- FreeRDP-2.4.0.orig/libfreerdp/codec/dsp.c
+++ FreeRDP-2.4.0/libfreerdp/codec/dsp.c
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include <winpr/crt.h>
+#include <winpr/assert.h>
 
 #include <freerdp/types.h>
 #include <freerdp/log.h>
@@ -294,9 +295,15 @@ static const INT16 ima_step_size_table[]
 
 static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* adpcm, unsigned int channel, BYTE sample)
 {
-	INT32 ss;
 	INT32 d;
-	ss = ima_step_size_table[adpcm->ima.last_step[channel]];
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
+
+	const INT16 offset = adpcm->ima.last_step[channel];
+	WINPR_ASSERT(offset >= 0);
+	WINPR_ASSERT(offset < ARRAYSIZE(ima_step_size_table));
+
+	const INT32 ss = ima_step_size_table[offset];
 	d = (ss >> 3);
 
 	if (sample & 1)
@@ -319,6 +326,7 @@ static UINT16 dsp_decode_ima_adpcm_sampl
 		d = 32767;
 
 	adpcm->ima.last_sample[channel] = (INT16)d;
+	WINPR_ASSERT(sample < ARRAYSIZE(ima_step_index_table));
 	adpcm->ima.last_step[channel] += ima_step_index_table[sample];
 
 	if (adpcm->ima.last_step[channel] < 0)
@@ -350,6 +358,9 @@ static BOOL freerdp_dsp_decode_ima_adpcm
 	{
 		if (size % block_size == 0)
 		{
+			if (size < 4)
+				return FALSE;
+
 			context->adpcm.ima.last_sample[0] =
 			    (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
 			context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
@@ -359,6 +370,8 @@ static BOOL freerdp_dsp_decode_ima_adpcm
 
 			if (channels > 1)
 			{
+				if (size < 4)
+					return FALSE;
 				context->adpcm.ima.last_sample[1] =
 				    (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
 				context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
@@ -370,6 +383,8 @@ static BOOL freerdp_dsp_decode_ima_adpcm
 
 		if (channels > 1)
 		{
+			if (size < 8)
+				return FALSE;
 			for (i = 0; i < 8; i++)
 			{
 				channel = (i < 4 ? 0 : 1);
@@ -389,6 +404,8 @@ static BOOL freerdp_dsp_decode_ima_adpcm
 		}
 		else
 		{
+			if (size < 1)
+				return FALSE;
 			sample = ((*src) & 0x0f);
 			decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
 			*dst++ = (decoded & 0xFF);
@@ -647,10 +664,16 @@ static BYTE dsp_encode_ima_adpcm_sample(
 {
 	INT32 e;
 	INT32 d;
-	INT32 ss;
 	BYTE enc;
 	INT32 diff;
-	ss = ima_step_size_table[adpcm->ima.last_step[channel]];
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
+
+	const INT16 offset = adpcm->ima.last_step[channel];
+	WINPR_ASSERT(offset >= 0);
+	WINPR_ASSERT(offset < ARRAYSIZE(ima_step_size_table));
+
+	INT32 ss = ima_step_size_table[offset];
 	d = e = sample - adpcm->ima.last_sample[channel];
 	diff = ss >> 3;
 	enc = 0;
@@ -696,6 +719,8 @@ static BYTE dsp_encode_ima_adpcm_sample(
 		diff = 32767;
 
 	adpcm->ima.last_sample[channel] = (INT16)diff;
+
+	WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
 	adpcm->ima.last_step[channel] += ima_step_index_table[enc];
 
 	if (adpcm->ima.last_step[channel] < 0)
@@ -790,12 +815,23 @@ static const INT32 ms_adpcm_coeffs2[7] =
 
 static INLINE INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* adpcm, BYTE sample, int channel)
 {
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
 	INT8 nibble;
 	INT32 presample;
 	nibble = (sample & 0x08 ? (INT8)sample - 16 : (INT8)sample);
-	presample = ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
-	             (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
-	            256;
+	const BYTE predictor = adpcm->ms.predictor[channel];
+	INT32 coeff1 = 0;
+	if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
+		coeff1 = ms_adpcm_coeffs1[predictor];
+
+	INT32 coeff2 = 0;
+	if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
+		coeff2 = ms_adpcm_coeffs2[predictor];
+	presample =
+        ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
 	presample += nibble * adpcm->ms.delta[channel];
 
 	if (presample > 32767)
@@ -805,7 +841,12 @@ static INLINE INT16 freerdp_dsp_decode_m
 
 	adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
 	adpcm->ms.sample1[channel] = presample;
-	adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[sample] / 256;
+
+	INT32 tableval = 0;
+	if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
+		tableval = ms_adpcm_adaptation_table[sample];
+
+	adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
 
 	if (adpcm->ms.delta[channel] < 16)
 		adpcm->ms.delta[channel] = 16;
@@ -833,6 +874,9 @@ static BOOL freerdp_dsp_decode_ms_adpcm(
 		{
 			if (channels > 1)
 			{
+				if (size < 14)
+					return FALSE;
+
 				context->adpcm.ms.predictor[0] = *src++;
 				context->adpcm.ms.predictor[1] = *src++;
 				context->adpcm.ms.delta[0] = read_int16(src);
@@ -859,6 +903,9 @@ static BOOL freerdp_dsp_decode_ms_adpcm(
 			}
 			else
 			{
+				if (size < 7)
+					return FALSE;
+
 				context->adpcm.ms.predictor[0] = *src++;
 				context->adpcm.ms.delta[0] = read_int16(src);
 				src += 2;
@@ -876,6 +923,8 @@ static BOOL freerdp_dsp_decode_ms_adpcm(
 
 		if (channels > 1)
 		{
+            if (size < 1)
+                return FALSE;
 			sample = *src++;
 			size--;
 			write_int16(dst, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
@@ -891,6 +940,8 @@ static BOOL freerdp_dsp_decode_ms_adpcm(
 		}
 		else
 		{
+            if (size < 1)
+                return FALSE;
 			sample = *src++;
 			size--;
 			write_int16(dst, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
@@ -904,8 +955,12 @@ static BOOL freerdp_dsp_decode_ms_adpcm(
 	return TRUE;
 }
 
-static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* adpcm, INT32 sample, int channel)
+static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* adpcm, INT32 sample, size_t channel)
 {
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
+	WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
 	INT32 presample;
 	INT32 errordelta;
 	presample = ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
@@ -930,8 +985,9 @@ static BYTE freerdp_dsp_encode_ms_adpcm_
 
 	adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
 	adpcm->ms.sample1[channel] = presample;
-	adpcm->ms.delta[channel] =
-	    adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[(((BYTE)errordelta) & 0x0F)] / 256;
+	const size_t offset = (((BYTE)errordelta) & 0x0F);
+	WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
+	adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
 
 	if (adpcm->ms.delta[channel] < 16)
 		adpcm->ms.delta[channel] = 16;
openSUSE Build Service is sponsored by