Skip to content

Commit 376dd57

Browse files
committed
Merge series "ASoC: SOF: fix kcontrol size checks" from Kai Vehmanen <kai.vehmanen@linux.intel.com>:
Series that fixes checks for 'size' in kcontrol get/put ext_bytes methods for SOF. The gaps in these checks were discovered via cppcheck warnings on unused variable values. Pierre-Louis Bossart (5): ASoC: SOF: control: fix size checks for ext_bytes control .get() ASoC: SOF: control: fix size checks for volatile ext_bytes control .get() ASoC: SOF: control: add size checks for ext_bytes control .put() ASoC: SOF: control: remove const in sizeof() ASoC: SOF: topology: remove const in sizeof() sound/soc/sof/control.c | 53 +++++++++++++++++++++++++++++++--------- sound/soc/sof/topology.c | 2 +- 2 files changed, 43 insertions(+), 12 deletions(-) -- 2.27.0
2 parents 163cd10 + 2ca2101 commit 376dd57

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

‎sound/soc/sof/control.c‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
300300
const struct snd_ctl_tlv __user *tlvd =
301301
(const struct snd_ctl_tlv __user *)binary_data;
302302

303+
/* make sure we have at least a header */
304+
if (size < sizeof(struct snd_ctl_tlv))
305+
return -EINVAL;
306+
303307
/*
304308
* The beginning of bytes data contains a header from where
305309
* the length (as bytes) is needed to know the correct copy
@@ -308,6 +312,13 @@ int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
308312
if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv)))
309313
return -EFAULT;
310314

315+
/* make sure TLV info is consistent */
316+
if (header.length + sizeof(struct snd_ctl_tlv) > size) {
317+
dev_err_ratelimited(scomp->dev, "error: inconsistent TLV, data %d + header %zu > %d\n",
318+
header.length, sizeof(struct snd_ctl_tlv), size);
319+
return -EINVAL;
320+
}
321+
311322
/* be->max is coming from topology */
312323
if (header.length > be->max) {
313324
dev_err_ratelimited(scomp->dev, "error: Bytes data size %d exceeds max %d.\n",
@@ -369,6 +380,14 @@ int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int _
369380
int ret;
370381
int err;
371382

383+
/*
384+
* Decrement the limit by ext bytes header size to
385+
* ensure the user space buffer is not exceeded.
386+
*/
387+
if (size < sizeof(struct snd_ctl_tlv))
388+
return -ENOSPC;
389+
size -= sizeof(struct snd_ctl_tlv);
390+
372391
ret = pm_runtime_get_sync(scomp->dev);
373392
if (ret < 0 && ret != -EACCES) {
374393
dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to resume %d\n", ret);
@@ -396,6 +415,12 @@ int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int _
396415

397416
data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
398417

418+
/* make sure we don't exceed size provided by user space for data */
419+
if (data_size > size) {
420+
ret = -ENOSPC;
421+
goto out;
422+
}
423+
399424
header.numid = scontrol->cmd;
400425
header.length = data_size;
401426
if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
@@ -432,7 +457,9 @@ int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
432457
* Decrement the limit by ext bytes header size to
433458
* ensure the user space buffer is not exceeded.
434459
*/
435-
size -= sizeof(const struct snd_ctl_tlv);
460+
if (size < sizeof(struct snd_ctl_tlv))
461+
return -ENOSPC;
462+
size -= sizeof(struct snd_ctl_tlv);
436463

437464
/* set the ABI header values */
438465
cdata->data->magic = SOF_ABI_MAGIC;
@@ -448,6 +475,10 @@ int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
448475

449476
data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
450477

478+
/* make sure we don't exceed size provided by user space for data */
479+
if (data_size > size)
480+
return -ENOSPC;
481+
451482
header.numid = scontrol->cmd;
452483
header.length = data_size;
453484
if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv)))

0 commit comments

Comments
 (0)