Skip to content

Commit d214508

Browse files
singalsulgirdwood
authored andcommitted
Tools: Testbench: Fix bytes control data validation in tb_send_bytes_data
The change in removing bytes control blob in module init() triggered an issue in sof-testbench4. It resulted in valgrind fail with scripts/host-testbench.sh run with error "Invalid read of size 1" in "memcpy(msg + sizeof(config), (char *)abi->data + offset, chunk_size)". The invalid read happens when abi->data doesn't have chunk_size bytes available. The fix is to skip bytes controls with no private data to avoid reading garbage abi->size from adjacent topology buffer data, which causes invalid memory reads in tb_send_bytes_data(). Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
1 parent 0885cc2 commit d214508

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

tools/testbench/topology_ipc4.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ static int tb_kcontrol_cb_new(struct snd_soc_tplg_ctl_hdr *tplg_ctl,
12861286
struct snd_soc_tplg_mixer_control *tplg_mixer;
12871287
struct snd_soc_tplg_enum_control *tplg_enum;
12881288
struct snd_soc_tplg_bytes_control *tplg_bytes;
1289+
struct sof_abi_hdr *abi;
12891290

12901291
if (glb->num_ctls >= TB_MAX_CTLS) {
12911292
fprintf(stderr, "Error: Too many controls already.\n");
@@ -1358,7 +1359,26 @@ static int tb_kcontrol_cb_new(struct snd_soc_tplg_ctl_hdr *tplg_ctl,
13581359
tplg_bytes->priv.size);
13591360
return -EINVAL;
13601361
}
1361-
ctl->data = tplg_bytes->priv.data;
1362+
1363+
if (tplg_bytes->priv.size >= sizeof(struct sof_abi_hdr)) {
1364+
abi = (struct sof_abi_hdr *)tplg_bytes->priv.data;
1365+
if (abi->size > TB_MAX_BYTES_DATA_SIZE) {
1366+
fprintf(stderr,
1367+
"Error: ABI payload size %u exceeds max %d\n",
1368+
abi->size, TB_MAX_BYTES_DATA_SIZE);
1369+
return -EINVAL;
1370+
}
1371+
if (tplg_bytes->priv.size <
1372+
sizeof(struct sof_abi_hdr) + abi->size) {
1373+
fprintf(stderr,
1374+
"Error: bytes data size %d is smaller than ABI header + payload (%zu + %u)\n",
1375+
tplg_bytes->priv.size,
1376+
sizeof(struct sof_abi_hdr), abi->size);
1377+
return -EINVAL;
1378+
}
1379+
ctl->data = tplg_bytes->priv.data;
1380+
}
1381+
13621382
ctl->comp_info = comp_info;
13631383
strncpy(ctl->name, tplg_ctl->name, TB_MAX_CTL_NAME_CHARS);
13641384
break;

tools/testbench/utils_ipc4.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ static int tb_set_up_widget(struct testbench_prm *tp, struct tplg_comp_info *com
290290
/* send the bytes data from kcontrols associated with current widget */
291291
if (ctl->module_id != comp_info->module_id ||
292292
ctl->instance_id != comp_info->instance_id ||
293-
ctl->type != SND_SOC_TPLG_TYPE_BYTES)
293+
ctl->type != SND_SOC_TPLG_TYPE_BYTES ||
294+
!ctl->data)
294295
continue;
295296

296297
abi = (struct sof_abi_hdr *)ctl->data;

0 commit comments

Comments
 (0)