Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put
@ 2026-11-19 12:14 Ye Weihua
  2026-11-19 12:14 ` [PATCH -next 2/2] ASoC: SOF: topology: Free bytes control backup on unload Ye Weihua
  2026-11-19 13:04 ` [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: Ye Weihua @ 2026-11-19 12:14 UTC (permalink / raw)
  To: Liam Girdwood, Peter Ujfalusi, Bard Liao, Daniel Baluta,
	Kai Vehmanen, Pierre-Louis Bossart, Vijendar Mukunda, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: sound-open-firmware, linux-sound, stable, Ye Weihua

sof_ipc4_bytes_ext_put() backs up the current control data with
kmemdup(..., scontrol->size) and updates scontrol->size to the size of
the new payload before sending it to the firmware. If the firmware
rejects the new payload, sof_ipc4_set_get_kcontrol_data() restores the
backup with memcpy(..., scontrol->size), reading past the end of the
backup buffer when the new payload is larger than the backed up one.

The mismatch is not limited to the current call: the backup is kept when
the update succeeds and is not refreshed by later updates, so
scontrol->size can grow beyond the size of the backup and a subsequent
failing update will trigger the same out-of-bounds read.

Store the size of the backup when it is created and use it to restore the
data and to update scontrol->size. Also only update scontrol->size after
the firmware update succeeded, as sof_ipc4_bytes_put() already does.

Restoring the local data is not enough to replay the last known good
configuration: the IPC message still carries the size of the rejected
payload. Reset the message size when its payload points into the restored
buffer, so the firmware receives the restored data with its own size.

Fixes: ebcfdbe4add9 ("ASoC: SOF: ipc4-control: Keep the payload size up to date")
Cc: stable@vger.kernel.org
Signed-off-by: Ye Weihua <yeweihua4@huawei.com>
---
 sound/soc/sof/ipc4-control.c | 26 +++++++++++++++++++++-----
 sound/soc/sof/sof-audio.h    |  1 +
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/sound/soc/sof/ipc4-control.c b/sound/soc/sof/ipc4-control.c
index e29c8d89284a..f8b4842f3936 100644
--- a/sound/soc/sof/ipc4-control.c
+++ b/sound/soc/sof/ipc4-control.c
@@ -56,20 +56,31 @@ static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
 	if (!set)
 		goto unlock;
 
 	/* It is a set-data operation, and we have a valid backup that we can restore */
 	if (ret < 0) {
+		struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
+
 		if (!scontrol->old_ipc_control_data)
 			goto unlock;
 		/*
 		 * Current ipc_control_data is not valid, we use the last known good
 		 * configuration
 		 */
 		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
-		       scontrol->size);
+		       scontrol->old_ipc_control_data_size);
+		scontrol->size = scontrol->old_ipc_control_data_size;
 		kfree(scontrol->old_ipc_control_data);
 		scontrol->old_ipc_control_data = NULL;
+		scontrol->old_ipc_control_data_size = 0;
+		/*
+		 * If the message payload points into the restored buffer, its size
+		 * must be restored as well, otherwise the last known good
+		 * configuration would be sent with the size of the rejected data.
+		 */
+		if (msg->data_ptr == cdata->data->data)
+			msg->data_size = cdata->data->size;
 		/* Send the last known good configuration to firmware */
 		ret = iops->set_get_data(sdev, msg, msg->data_size, set);
 		if (ret < 0)
 			goto unlock;
 	}
@@ -628,10 +639,11 @@ static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
 	struct snd_soc_component *scomp = scontrol->scomp;
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 	struct sof_abi_hdr *data = cdata->data;
 	struct sof_abi_hdr abi_hdr;
 	struct snd_ctl_tlv header;
+	int ret;
 
 	/*
 	 * The beginning of bytes data contains a header from where
 	 * the length (as bytes) is needed to know the correct copy
 	 * length of data from tlvd->tlv.
@@ -684,25 +696,29 @@ static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
 		/* Create a backup of the current, valid bytes control */
 		scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
 							 scontrol->size, GFP_KERNEL);
 		if (!scontrol->old_ipc_control_data)
 			return -ENOMEM;
+		scontrol->old_ipc_control_data_size = scontrol->size;
 	}
 
 	/* Copy the whole binary data which includes the ABI header and the payload */
 	if (copy_from_user(data, tlvd->tlv, header.length)) {
 		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
-		       scontrol->size);
+		       scontrol->old_ipc_control_data_size);
 		kfree(scontrol->old_ipc_control_data);
 		scontrol->old_ipc_control_data = NULL;
+		scontrol->old_ipc_control_data_size = 0;
 		return -EFAULT;
 	}
 
-	/* Update the cdata size */
-	scontrol->size = sizeof(*cdata) + header.length;
+	ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
+	if (!ret)
+		/* Update the cdata size */
+		scontrol->size = sizeof(*cdata) + header.length;
 
-	return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
+	return ret;
 }
 
 static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
 				   const unsigned int __user *binary_data,
 				   unsigned int size, bool from_dsp)
diff --git a/sound/soc/sof/sof-audio.h b/sound/soc/sof/sof-audio.h
index 672879371d14..c563e7b93f5a 100644
--- a/sound/soc/sof/sof-audio.h
+++ b/sound/soc/sof/sof-audio.h
@@ -391,10 +391,11 @@ struct snd_sof_control {
 	void *priv; /* private data copied from topology */
 	size_t priv_size; /* size of private data */
 	size_t max_size;
 	void *ipc_control_data;
 	void *old_ipc_control_data;
+	size_t old_ipc_control_data_size;
 	int max; /* applicable to volume controls */
 	u32 size;	/* cdata size */
 	u32 *volume_table; /* volume table computed from tlv data*/
 
 	struct list_head list;	/* list in sdev control list */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH -next 2/2] ASoC: SOF: topology: Free bytes control backup on unload
  2026-11-19 12:14 [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put Ye Weihua
@ 2026-11-19 12:14 ` Ye Weihua
  2026-11-19 13:04 ` [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: Ye Weihua @ 2026-11-19 12:14 UTC (permalink / raw)
  To: Liam Girdwood, Peter Ujfalusi, Bard Liao, Daniel Baluta,
	Kai Vehmanen, Pierre-Louis Bossart, Vijendar Mukunda, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: sound-open-firmware, linux-sound, stable, Ye Weihua

sof_ipc4_bytes_ext_put() and sof_ipc3_bytes_ext_put() keep a backup of the
last known good bytes control data in scontrol->old_ipc_control_data.
This backup is only released when it is restored, so it is leaked when
the control is freed while the backup is still present.

Free old_ipc_control_data in sof_control_unload() and
sof_widget_unload(), which are the two paths that release the control
data, along with the rest of the control data.

Fixes: 299f6c752f8f ("ASoC: sof: Improve sof_ipc3_bytes_ext_put function")
Cc: stable@vger.kernel.org
Signed-off-by: Ye Weihua <yeweihua4@huawei.com>
---
 sound/soc/sof/topology.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c
index 4dcacafb9cdb..c21a0199db9c 100644
--- a/sound/soc/sof/topology.c
+++ b/sound/soc/sof/topology.c
@@ -1069,10 +1069,11 @@ static int sof_control_unload(struct snd_soc_component *scomp,
 			dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
 	}
 
 	/* free all data before returning in case of error too */
 	kfree(scontrol->ipc_control_data);
+	kfree(scontrol->old_ipc_control_data);
 	kfree(scontrol->priv);
 	kfree(scontrol->name);
 	list_del(&scontrol->list);
 	kfree(scontrol);
 
@@ -1717,10 +1718,11 @@ static int sof_widget_unload(struct snd_soc_component *scomp,
 		default:
 			dev_warn(scomp->dev, "unsupported kcontrol_type\n");
 			goto out;
 		}
 		kfree(scontrol->ipc_control_data);
+		kfree(scontrol->old_ipc_control_data);
 		list_del(&scontrol->list);
 		kfree(scontrol->name);
 		kfree(scontrol);
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put
  2026-11-19 12:14 [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put Ye Weihua
  2026-11-19 12:14 ` [PATCH -next 2/2] ASoC: SOF: topology: Free bytes control backup on unload Ye Weihua
@ 2026-11-19 13:04 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-19 13:04 UTC (permalink / raw)
  To: Ye Weihua; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ASoC: SOF: restored scontrol->size overwritten in
  sof_ipc4_bytes_ext_put()
- [Medium] ASoC: SOF: scontrol->size not restored on EFAULT in
  sof_ipc4_bytes_ext_put()
- [Medium] ASoC: SOF: ipc4-control: incomplete backup restore when
  priv_size is 0
- [Medium] ASoC: SOF: ipc4-control: incomplete msg restore in
  sof_ipc4_set_get_kcontrol_data()
--

commit 3d54ef5fbf4ff729417838f0907d7062db9c4154
Author: Ye Weihua <yeweihua4@huawei.com>

ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put

This commit records the backup size in old_ipc_control_data_size to avoid an
out-of-bounds read when restoring bytes control data, restores msg->data_size
when replaying the backup, and moves the scontrol->size update in
sof_ipc4_bytes_ext_put() until after sof_ipc4_set_get_bytes_data() returns.

> diff --git a/sound/soc/sof/ipc4-control.c b/sound/soc/sof/ipc4-control.c
> index e29c8d89284af..f8b4842f39365 100644
> --- a/sound/soc/sof/ipc4-control.c
> +++ b/sound/soc/sof/ipc4-control.c
> @@ -58,6 +58,8 @@ static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
>  
>  	/* It is a set-data operation, and we have a valid backup that we can restore */
>  	if (ret < 0) {
> +		struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
> +
>  		if (!scontrol->old_ipc_control_data)
>  			goto unlock;
>  		/*
> @@ -65,9 +67,18 @@ static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
>  		 * configuration
>  		 */
>  		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
> -		       scontrol->size);
> +		       scontrol->old_ipc_control_data_size);
> +		scontrol->size = scontrol->old_ipc_control_data_size;
>  		kfree(scontrol->old_ipc_control_data);
>  		scontrol->old_ipc_control_data = NULL;
> +		scontrol->old_ipc_control_data_size = 0;
> +		/*
> +		 * If the message payload points into the restored buffer, its size
> +		 * must be restored as well, otherwise the last known good
> +		 * configuration would be sent with the size of the rejected data.
> +		 */
> +		if (msg->data_ptr == cdata->data->data)
> +			msg->data_size = cdata->data->size;
>  		/* Send the last known good configuration to firmware */
>  		ret = iops->set_get_data(sdev, msg, msg->data_size, set);
>  		if (ret < 0)

[Severity: Medium]
Does checking msg->data_ptr == cdata->data->data miss generic bytes controls
(SOF_IPC4_BYTES_CONTROL_PARAM_ID) and leave msg->extension un-restored?

When data->type == SOF_IPC4_BYTES_CONTROL_PARAM_ID,
sof_ipc4_set_get_bytes_data() calls sof_ipc4_set_bytes_control_data(), which
copies the new payload into a separate msg_data buffer and sets
msg.data_ptr = msg_data:

sound/soc/sof/ipc4-control.c:sof_ipc4_set_bytes_control_data() {
	...
	msg_data->id = cdata->index;
	msg_data->num_elems = data->size;
	memcpy(msg_data->data, data->data, data->size);

	memcpy(&msg, &cdata->msg, sizeof(msg));
	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);

	msg.data_ptr = msg_data;
	msg.data_size = data_size;

	ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, true, lock);
	...
}

Because msg->data_ptr points to msg_data rather than cdata->data->data, the
replay call re-sends the rejected msg_data buffer and size instead of the
restored backup.  Even when msg->data_ptr == cdata->data->data,
sof_ipc4_bytes_ext_put() overwrites data->type from user space before
sof_ipc4_set_get_bytes_data() sets msg.extension, so msg->extension is not
restored from the backed-up cdata->data->type either.

> @@ -630,6 +641,7 @@ static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
>  	struct sof_abi_hdr *data = cdata->data;
>  	struct sof_abi_hdr abi_hdr;
>  	struct snd_ctl_tlv header;
> +	int ret;
>  
>  	/*
>  	 * The beginning of bytes data contains a header from where
> @@ -686,21 +698,25 @@ static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
>  							 scontrol->size, GFP_KERNEL);
>  		if (!scontrol->old_ipc_control_data)
>  			return -ENOMEM;
> +		scontrol->old_ipc_control_data_size = scontrol->size;
>  	}

[Severity: Medium]
Can backing up and restoring scontrol->size bytes leave cdata->data
un-restored when a bytes control has no initial topology payload?

In sof_ipc4_control_load_bytes(), scontrol->size is initialized to
sizeof(*control_data) + scontrol->priv_size.  When scontrol->priv_size is 0,
scontrol->size is only sizeof(*cdata), which stops before the flexible array
member cdata->data (struct sof_abi_hdr).  kmemdup() then backs up 0 bytes of
cdata->data, and on failure of copy_from_user() or iops->set_get_data(),
memcpy(..., scontrol->old_ipc_control_data_size) restores only sizeof(*cdata)
bytes, leaving the rejected ABI header and payload in cdata->data and
replaying the rejected cdata->data->size and payload to the firmware.

>  	/* Copy the whole binary data which includes the ABI header and the payload */
>  	if (copy_from_user(data, tlvd->tlv, header.length)) {
>  		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
> -		       scontrol->size);
> +		       scontrol->old_ipc_control_data_size);
>  		kfree(scontrol->old_ipc_control_data);
>  		scontrol->old_ipc_control_data = NULL;
> +		scontrol->old_ipc_control_data_size = 0;
>  		return -EFAULT;
>  	}

[Severity: Medium]
Should this error path also restore
scontrol->size = scontrol->old_ipc_control_data_size, like
sof_ipc4_set_get_kcontrol_data() does?

Because scontrol->old_ipc_control_data is kept across successful updates
while later updates change scontrol->size, a copy_from_user() failure on a
subsequent call restores the older backup into scontrol->ipc_control_data
without resetting scontrol->size, causing the next sof_ipc4_bytes_ext_put()
to kmemdup() the wrong size.

> -	/* Update the cdata size */
> -	scontrol->size = sizeof(*cdata) + header.length;
> +	ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
> +	if (!ret)
> +		/* Update the cdata size */
> +		scontrol->size = sizeof(*cdata) + header.length;
>  
> -	return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
> +	return ret;
>  }

[Severity: Medium]
Does this overwrite the restored scontrol->size with the rejected payload
size whenever the backup replay succeeds?

When the firmware rejects the new payload in
sof_ipc4_set_get_kcontrol_data(), the recovery path restores
scontrol->size = scontrol->old_ipc_control_data_size, frees
scontrol->old_ipc_control_data, and calls:

	ret = iops->set_get_data(sdev, msg, msg->data_size, set);

When replaying the backup configuration succeeds, ret becomes 0 and
sof_ipc4_set_get_bytes_data() returns 0.  Then if (!ret) here overwrites the
restored scontrol->size with sizeof(*cdata) + header.length from the rejected
request, so the next sof_ipc4_bytes_ext_put() backs up the wrong size.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119121446.124833-1-yeweihua4@huawei.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-19 13:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 12:14 [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put Ye Weihua
2026-11-19 12:14 ` [PATCH -next 2/2] ASoC: SOF: topology: Free bytes control backup on unload Ye Weihua
2026-11-19 13:04 ` [PATCH -next 1/2] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox