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