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