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 --- 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 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 --- 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 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 --- 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