Skip to content

Commit a962890

Browse files
ujfalusibroonie
authored andcommitted
ASoC: SOF: ipc3-topology: Correct get_control_data for non bytes payload
It is possible to craft a topology where sof_get_control_data() would do out of bounds access because it expects that it is only called when the payload is bytes type. Confusingly it also handles other types of controls, but the payload parsing implementation is only valid for bytes. Fix the code to count the non bytes controls and instead of storing a pointer to sof_abi_hdr in sof_widget_data (which is only valid for bytes), store the pointer to the data itself and add a new member to save the size of the data. In case of non bytes controls we store the pointer to the chanv itself, which is just an array of values at the end. In case of bytes control, drop the wrong cdata->data (wdata[i].pdata) check against NULL since it is incorrect and invalid in this context. The data is pointing to the end of cdata struct, so it should never be null. Reported-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Peter Ujfalusi <[email protected]> Reviewed-by: Sergey Senozhatsky <[email protected]> Tested-by: Sergey Senozhatsky <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 4213ff5 commit a962890

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

sound/soc/sof/ipc3-topology.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
struct sof_widget_data {
2121
int ctrl_type;
2222
int ipc_cmd;
23-
struct sof_abi_hdr *pdata;
23+
void *pdata;
24+
size_t pdata_size;
2425
struct snd_sof_control *control;
2526
};
2627

@@ -784,16 +785,26 @@ static int sof_get_control_data(struct snd_soc_component *scomp,
784785
}
785786

786787
cdata = wdata[i].control->ipc_control_data;
787-
wdata[i].pdata = cdata->data;
788-
if (!wdata[i].pdata)
789-
return -EINVAL;
790788

791-
/* make sure data is valid - data can be updated at runtime */
792-
if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES &&
793-
wdata[i].pdata->magic != SOF_ABI_MAGIC)
794-
return -EINVAL;
789+
if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES) {
790+
/* make sure data is valid - data can be updated at runtime */
791+
if (cdata->data->magic != SOF_ABI_MAGIC)
792+
return -EINVAL;
793+
794+
wdata[i].pdata = cdata->data->data;
795+
wdata[i].pdata_size = cdata->data->size;
796+
} else {
797+
/* points to the control data union */
798+
wdata[i].pdata = cdata->chanv;
799+
/*
800+
* wdata[i].control->size is calculated with struct_size
801+
* and includes the size of struct sof_ipc_ctrl_data
802+
*/
803+
wdata[i].pdata_size = wdata[i].control->size -
804+
sizeof(struct sof_ipc_ctrl_data);
805+
}
795806

796-
*size += wdata[i].pdata->size;
807+
*size += wdata[i].pdata_size;
797808

798809
/* get data type */
799810
switch (cdata->cmd) {
@@ -876,10 +887,12 @@ static int sof_process_load(struct snd_soc_component *scomp,
876887
*/
877888
if (ipc_data_size) {
878889
for (i = 0; i < widget->num_kcontrols; i++) {
879-
memcpy(&process->data[offset],
880-
wdata[i].pdata->data,
881-
wdata[i].pdata->size);
882-
offset += wdata[i].pdata->size;
890+
if (!wdata[i].pdata_size)
891+
continue;
892+
893+
memcpy(&process->data[offset], wdata[i].pdata,
894+
wdata[i].pdata_size);
895+
offset += wdata[i].pdata_size;
883896
}
884897
}
885898

0 commit comments

Comments
 (0)