* [PATCH v2 1/3] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put
2026-11-20 7:00 [PATCH v2 0/3] ASoC: SOF: ipc4-control: fix bytes control restore and replay Ye Weihua
@ 2026-11-20 7:00 ` Ye Weihua
2026-11-20 7:00 ` [PATCH v2 2/3] ASoC: SOF: topology: Free bytes control backup on unload Ye Weihua
2026-11-20 7:00 ` [PATCH v2 3/3] ASoC: SOF: ipc4-control: Rebuild replay message after restoring data Ye Weihua
2 siblings, 0 replies; 5+ messages in thread
From: Ye Weihua @ 2026-11-20 7:00 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 ipc_control_data with
kmemdup(..., scontrol->size), but scontrol->size is the current payload
size, not the allocation size. Since the backup is kept after a
successful update, scontrol->size can grow beyond it. When a later update
is rejected, sof_ipc4_set_get_kcontrol_data() restores with
memcpy(..., scontrol->size), reading past the end of the backup.
Back up and restore the whole allocation (sizeof(*cdata) + max_size),
like IPC3. This also restores the ABI header and payload for controls
loaded without initial data. Recompute scontrol->size from the restored
payload on every restore, including the copy_from_user() failure path,
and only update it after the firmware accepts the new data.
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 | 42 +++++++++++++++++++++++++++---------
1 file changed, 32 insertions(+), 10 deletions(-)
diff --git a/sound/soc/sof/ipc4-control.c b/sound/soc/sof/ipc4-control.c
index e29c8d89284a..ace3bf2f719d 100644
--- a/sound/soc/sof/ipc4-control.c
+++ b/sound/soc/sof/ipc4-control.c
@@ -56,18 +56,25 @@ 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
+ * Current ipc_control_data is not valid, we use the last known
+ * good configuration. The backup covers the whole allocated
+ * control data, so the ABI header and payload are restored even
+ * when the control was loaded without initial data.
*/
memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
- scontrol->size);
+ sizeof(*cdata) + scontrol->max_size);
+ /* Keep the size consistent with the restored control data */
+ scontrol->size = sizeof(*cdata) + sizeof(*cdata->data) +
+ cdata->data->size;
kfree(scontrol->old_ipc_control_data);
scontrol->old_ipc_control_data = NULL;
/* Send the last known good configuration to firmware */
ret = iops->set_get_data(sdev, msg, msg->data_size, set);
if (ret < 0)
@@ -628,10 +635,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.
@@ -679,30 +687,44 @@ static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
abi_hdr.size, scontrol->max_size - sizeof(abi_hdr));
return -EINVAL;
}
if (!scontrol->old_ipc_control_data) {
- /* Create a backup of the current, valid bytes control */
- scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
- scontrol->size, GFP_KERNEL);
+ /*
+ * Create a backup of the current, valid bytes control. Cover
+ * the whole allocated control data, including the ABI header
+ * and payload, so that the last known good configuration can
+ * always be restored, even when the control has no initial
+ * data (priv_size == 0).
+ */
+ scontrol->old_ipc_control_data =
+ kmemdup(scontrol->ipc_control_data,
+ sizeof(*cdata) + scontrol->max_size, GFP_KERNEL);
if (!scontrol->old_ipc_control_data)
return -ENOMEM;
}
/* 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);
+ sizeof(*cdata) + scontrol->max_size);
+ scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;
kfree(scontrol->old_ipc_control_data);
scontrol->old_ipc_control_data = NULL;
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. Use the payload size from the control
+ * data so that a restore of the last known good configuration
+ * does not get overwritten with the size of rejected data.
+ */
+ scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;
- 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)
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/3] ASoC: SOF: topology: Free bytes control backup on unload
2026-11-20 7:00 [PATCH v2 0/3] ASoC: SOF: ipc4-control: fix bytes control restore and replay Ye Weihua
2026-11-20 7:00 ` [PATCH v2 1/3] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put Ye Weihua
@ 2026-11-20 7:00 ` Ye Weihua
2026-11-20 7:00 ` [PATCH v2 3/3] ASoC: SOF: ipc4-control: Rebuild replay message after restoring data Ye Weihua
2 siblings, 0 replies; 5+ messages in thread
From: Ye Weihua @ 2026-11-20 7:00 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
scontrol->old_ipc_control_data is only freed when it is restored, so it
leaks if the control is released while the backup is present. Free it in
sof_control_unload() and sof_widget_unload() 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] 5+ messages in thread* [PATCH v2 3/3] ASoC: SOF: ipc4-control: Rebuild replay message after restoring data
2026-11-20 7:00 [PATCH v2 0/3] ASoC: SOF: ipc4-control: fix bytes control restore and replay Ye Weihua
2026-11-20 7:00 ` [PATCH v2 1/3] ASoC: SOF: ipc4-control: Fix out-of-bounds backup restore in bytes ext put Ye Weihua
2026-11-20 7:00 ` [PATCH v2 2/3] ASoC: SOF: topology: Free bytes control backup on unload Ye Weihua
@ 2026-11-20 7:00 ` Ye Weihua
2026-11-20 7:42 ` sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: Ye Weihua @ 2026-11-20 7:00 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
When a set-data request is rejected, sof_ipc4_set_get_kcontrol_data()
restores the last known good data but replays the message built from the
rejected data. For regular bytes controls the extension may come from the
rejected data; for SOF_IPC4_BYTES_CONTROL_PARAM_ID controls the payload
lives in a separate buffer that still holds the rejected data. The
firmware therefore does not receive the restored configuration.
Let the caller rebuild the message from the restored data and replay it.
Move the param-id message setup into a helper so the payload can be
reallocated with the restored size.
Fixes: db38d86d0c54 ("ASoC: sof: Improve sof_ipc4_bytes_ext_put function")
Cc: stable@vger.kernel.org
Signed-off-by: Ye Weihua <yeweihua4@huawei.com>
---
sound/soc/sof/ipc4-control.c | 53 ++++++++++++++++++++++++++++++------
1 file changed, 45 insertions(+), 8 deletions(-)
diff --git a/sound/soc/sof/ipc4-control.c b/sound/soc/sof/ipc4-control.c
index ace3bf2f719d..8ed006efe17f 100644
--- a/sound/soc/sof/ipc4-control.c
+++ b/sound/soc/sof/ipc4-control.c
@@ -73,14 +73,14 @@ static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
/* Keep the size consistent with the restored control data */
scontrol->size = sizeof(*cdata) + sizeof(*cdata->data) +
cdata->data->size;
kfree(scontrol->old_ipc_control_data);
scontrol->old_ipc_control_data = NULL;
- /* Send the last known good configuration to firmware */
- ret = iops->set_get_data(sdev, msg, msg->data_size, set);
- if (ret < 0)
- goto unlock;
+ /*
+ * The caller is responsible for rebuilding the message from
+ * the restored control data and replaying it.
+ */
}
unlock:
if (lock)
mutex_unlock(&swidget->setup_mutex);
@@ -287,14 +287,13 @@ static void sof_ipc4_refresh_generic_control(struct snd_sof_control *scontrol)
kfree(data);
}
static int
-sof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
+sof_ipc4_send_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
{
struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
- struct snd_soc_component *scomp = scontrol->scomp;
struct sof_ipc4_control_msg_payload *msg_data;
struct sof_abi_hdr *data = cdata->data;
struct sof_ipc4_msg msg;
size_t data_size;
int ret;
@@ -313,16 +312,39 @@ sof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
msg.data_ptr = msg_data;
msg.data_size = data_size;
ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, true, lock);
+
+ kfree(msg_data);
+
+ return ret;
+}
+
+static int
+sof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
+{
+ struct snd_soc_component *scomp = scontrol->scomp;
+ void *old_ipc_control_data;
+ int ret;
+
+ old_ipc_control_data = scontrol->old_ipc_control_data;
+ ret = sof_ipc4_send_bytes_control_data(scontrol, lock);
+ if (ret < 0 && old_ipc_control_data &&
+ !scontrol->old_ipc_control_data) {
+ /*
+ * The new data was rejected and the last known good
+ * configuration was restored. Rebuild the message from the
+ * restored control data and replay it.
+ */
+ ret = sof_ipc4_send_bytes_control_data(scontrol, lock);
+ }
+
if (ret < 0)
dev_err(scomp->dev, "%s: Failed to set control update for %s\n",
__func__, scontrol->name);
- kfree(msg_data);
-
return ret;
}
static int
sof_ipc4_refresh_bytes_control(struct snd_sof_control *scontrol, bool lock)
@@ -508,10 +530,11 @@ static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
bool set, bool lock)
{
struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
struct sof_abi_hdr *data = cdata->data;
struct sof_ipc4_msg msg;
+ void *old_ipc_control_data;
int ret = 0;
/* Send the new data to the firmware only if it is powered up */
if (set) {
if (!pm_runtime_active(sdev->dev))
@@ -538,11 +561,25 @@ static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
if (set)
msg.data_size = data->size;
else
msg.data_size = scontrol->max_size - sizeof(*data);
+ old_ipc_control_data = scontrol->old_ipc_control_data;
ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, set, lock);
+ if (ret < 0 && set && old_ipc_control_data &&
+ !scontrol->old_ipc_control_data) {
+ /*
+ * The new data was rejected and the last known good
+ * configuration was restored. Rebuild the message from the
+ * restored control data and replay it.
+ */
+ msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
+ msg.data_ptr = data->data;
+ msg.data_size = data->size;
+ ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, set, lock);
+ }
+
if (ret < 0) {
dev_err(sdev->dev, "Failed to %s for %s\n",
set ? "set bytes update" : "get bytes",
scontrol->name);
} else if (!set) {
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2 3/3] ASoC: SOF: ipc4-control: Rebuild replay message after restoring data
2026-11-20 7:00 ` [PATCH v2 3/3] ASoC: SOF: ipc4-control: Rebuild replay message after restoring data Ye Weihua
@ 2026-11-20 7:42 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-20 7:42 UTC (permalink / raw)
To: Ye Weihua; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ASoC: SOF: ipc4: missing size and type check in
sof_ipc4_set_get_bytes_data()
--
commit 5bb9387ebbc31097ff7ba75b9019b86b70c0a08a
Author: Ye Weihua <yeweihua4@huawei.com>
ASoC: SOF: ipc4-control: Rebuild replay message after restoring data
This commit moves the IPC replay after a rejected set-data request from
sof_ipc4_set_get_kcontrol_data() to its callers so that the message and
payload are rebuilt from the restored backup data.
> diff --git a/sound/soc/sof/ipc4-control.c b/sound/soc/sof/ipc4-control.c
> index ace3bf2f719d3..8ed006efe17f3 100644
> --- a/sound/soc/sof/ipc4-control.c
> +++ b/sound/soc/sof/ipc4-control.c
[ ... ]
> @@ -315,12 +314,35 @@ sof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
> msg.data_size = data_size;
>
> ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, true, lock);
> +
> + kfree(msg_data);
> +
> + return ret;
> +}
> +
> +static int
> +sof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
> +{
> + struct snd_soc_component *scomp = scontrol->scomp;
> + void *old_ipc_control_data;
> + int ret;
> +
> + old_ipc_control_data = scontrol->old_ipc_control_data;
> + ret = sof_ipc4_send_bytes_control_data(scontrol, lock);
> + if (ret < 0 && old_ipc_control_data &&
> + !scontrol->old_ipc_control_data) {
> + /*
> + * The new data was rejected and the last known good
> + * configuration was restored. Rebuild the message from the
> + * restored control data and replay it.
> + */
> + ret = sof_ipc4_send_bytes_control_data(scontrol, lock);
> + }
> +
> if (ret < 0)
> dev_err(scomp->dev, "%s: Failed to set control update for %s\n",
> __func__, scontrol->name);
>
> - kfree(msg_data);
> -
> return ret;
> }
>
> @@ -510,6 +532,7 @@ static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
> struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
> struct sof_abi_hdr *data = cdata->data;
> struct sof_ipc4_msg msg;
> + void *old_ipc_control_data;
> int ret = 0;
>
> /* Send the new data to the firmware only if it is powered up */
> @@ -540,7 +563,21 @@ static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
> else
> msg.data_size = scontrol->max_size - sizeof(*data);
>
> + old_ipc_control_data = scontrol->old_ipc_control_data;
> ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, set, lock);
> + if (ret < 0 && set && old_ipc_control_data &&
> + !scontrol->old_ipc_control_data) {
> + /*
> + * The new data was rejected and the last known good
> + * configuration was restored. Rebuild the message from the
> + * restored control data and replay it.
> + */
> + msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
> + msg.data_ptr = data->data;
> + msg.data_size = data->size;
> + ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, set, lock);
> + }
[Severity: Medium]
Does replaying inside sof_ipc4_set_bytes_control_data() and
sof_ipc4_set_get_bytes_data() skip the !data->size and data->type checks
that sof_ipc4_set_get_bytes_data() only performs before the first send?
When a bytes control is loaded from topology without initial data
(scontrol->priv_size == 0), sof_ipc4_bytes_ext_put() backs up the
zero-initialized cdata->data (data->size == 0, data->type == 0) into
scontrol->old_ipc_control_data. If the firmware rejects an update and
sof_ipc4_set_get_kcontrol_data() restores that empty backup, both replay
paths unconditionally send a 0-byte MOD_LARGE_CONFIG_SET IPC with
param_id 0 instead of returning early as the initial !data->size check
does. Likewise, if the rejected user payload and the restored backup
differ on whether data->type is SOF_IPC4_BYTES_CONTROL_PARAM_ID, the
replay stays in the branch chosen by the rejected data->type and sends
the restored payload in the wrong format.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120070042.279508-1-yeweihua4@huawei.com?part=3
^ permalink raw reply [flat|nested] 5+ messages in thread