ASoC: SOF: ipc3/ipc4-control: harden kcontrol payload handling
Subsystems (4)
Peter Ujfalusi
maintainercover ASoC: SOF: ipc3/ipc4-control: harden kcontrol payload handling
Hi,
This series hardens SOF kcontrol data paths for both IPC3 and IPC4 by fixing size-handling bugs in put/get/update flows and tightening bounds checks around firmware/user-provided payload lengths.
The changes include:
Fix TOCTOU-style size misuse in IPC3/IPC4 bytes put paths by validating and using the incoming payload size.
Add notification/update payload size validation before parsing control data. Use overflow-checked arithmetic when computing expected IPC3 control sizes. Ensure update/copy bounds are validated against actual allocation limits. Fix IPC3 bytes_ext bounds checks to account for struct header offset, closing a heap overflow/over-read issue from unprivileged userspace TLV access. Overall, the series makes control payload processing robust against malformed or inconsistent sizes and prevents out-of-bounds accesses.
Regards,
Peter
---
Peter Ujfalusi (6):
ASoC: SOF: ipc4-control: Fix TOCTOU in sof_ipc4_bytes_put
ASoC: SOF: ipc4-control: Validate notification payload size
ASoC: SOF: ipc3-control: Use overflow checks in control_update size
calc
ASoC: SOF: ipc3-control: Validate size in snd_sof_update_control
ASoC: SOF: ipc3-control: Fix TOCTOU in bytes_put and bytes_get
ASoC: SOF: ipc3-control: Fix heap overflow in bytes_ext put/get
sound/soc/sof/ipc3-control.c | 79 +++++++++++++++++++++++++++--------- sound/soc/sof/ipc4-control.c | 34 ++++++++++++++-- 2 files changed, 90 insertions(+), 23 deletions(-)
Signature · 1 line
2.54.0
Peter Ujfalusi
maintainerPATCH 1/6 ASoC: SOF: ipc4-control: Fix TOCTOU in sof_ipc4_bytes_put
In sof_ipc4_bytes_put(), the copy size is derived from the old data->size in the buffer rather than the incoming new data's size field from ucontrol. If the new data has a different size, the copy uses the wrong length: it may truncate valid data or copy stale bytes.
Fix by validating and using the incoming data's sof_abi_hdr.size from ucontrol before copying.
--- sound/soc/sof/ipc4-control.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
sound/soc/sof/ipc4-control.c +7 -4
@@ -554,6 +554,8 @@ static int sof_ipc4_bytes_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; +const struct sof_abi_hdr *new_hdr = + (const struct sof_abi_hdr *)ucontrol->value.bytes.data;size_t size;int ret;
@@ -564,15 +566,16 @@ static int sof_ipc4_bytes_put(struct snd_sof_control *scontrol,return -EINVAL ; } -/* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */ -if (data->size > scontrol->max_size -sizeof (*data)) { +/* Validate the new data's size, not the old one */ +if (new_hdr->size > scontrol->max_size -sizeof (*new_hdr)) {dev_err_ratelimited (scomp->dev,"data size too big %u bytes max is %zu\n" , - data->size, scontrol->max_size -sizeof (*data)); + new_hdr->size, + scontrol->max_size -sizeof (*new_hdr));return -EINVAL ; } - size = data->size +sizeof (*data); + size = new_hdr->size +sizeof (*new_hdr);/* copy from kcontrol */ memcpy (data, ucontrol->value.bytes.data, size);
Signature · 1 line
2.54.0
Peter Ujfalusi
maintainerPATCH 2/6 ASoC: SOF: ipc4-control: Validate notification payload size
In reply to Peter UjfalusiValidate MODULE_NOTIFICATION payload length before reading bytes/channel data in control update handling.
--- sound/soc/sof/ipc4-control.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
sound/soc/sof/ipc4-control.c +23 -0
@@ -875,6 +875,16 @@ static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message)*/ if (type ==SND_SOC_TPLG_TYPE_BYTES ) {struct sof_abi_hdr *data = cdata->data; +size_t source_size =struct_size (msg_data, data, msg_data->num_elems); + +if (source_size > ndata->event_data_size) { +dev_warn (sdev->dev, +"%s: invalid bytes notification size for %s (%zu, %u)\n" , + __func__, scontrol->name, source_size, + ndata->event_data_size); + scontrol->comp_data_dirty = true; + goto notify; + }if (msg_data->num_elems > scontrol->max_size -sizeof (*data)) { dev_warn(sdev->dev,
@@ -887,6 +897,17 @@ static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message) scontrol->size =sizeof (*cdata) +sizeof (*data) + data->size; } }else { +size_t source_size =struct_size (msg_data, chanv, msg_data->num_elems); + +if (source_size > ndata->event_data_size) { +dev_warn (sdev->dev, +"%s: invalid channel notification size for %s (%zu, %u)\n" , + __func__, scontrol->name, source_size, + ndata->event_data_size); + scontrol->comp_data_dirty = true; + goto notify; + } +for (i =0 ; i < msg_data->num_elems; i++) {u32 channel = msg_data->chanv[i].channel;
@@ -914,6 +935,8 @@ static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message) scontrol->comp_data_dirty = true; } +notify: +/* * Look up the ALSA kcontrol of the scontrol to be able to send a * notification to user space
Signature · 1 line
2.54.0
Peter Ujfalusi
maintainerPATCH 3/6 ASoC: SOF: ipc3-control: Use overflow checks in control_update size calc
In reply to Peter UjfalusiIn sof_ipc3_control_update(), the expected_size calculation uses firmware-provided cdata->num_elems in arithmetic that could overflow on 32-bit platforms, wrapping to a small value. This would allow the cdata->rhdr.hdr.size comparison to pass with mismatched sizes, potentially leading to out-of-bounds access in snd_sof_update_control.
Use check_mul_overflow() and check_add_overflow() to detect and reject overflowed size calculations.
--- sound/soc/sof/ipc3-control.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
sound/soc/sof/ipc3-control.c +16 -4
@@ -626,16 +626,28 @@ static void sof_ipc3_control_update(struct snd_sof_dev *sdev, void *ipc_control_return ; } - expected_size =sizeof (struct sof_ipc_ctrl_data );switch (cdata->type) {case SOF_CTRL_TYPE_VALUE_CHAN_GET :case SOF_CTRL_TYPE_VALUE_CHAN_SET : - expected_size += cdata->num_elems * -sizeof (struct sof_ipc_ctrl_value_chan ); +if (check_mul_overflow ((size_t )cdata->num_elems, +sizeof (struct sof_ipc_ctrl_value_chan ), + &expected_size)) +return ; +if (check_add_overflow (expected_size, +sizeof (struct sof_ipc_ctrl_data ), + &expected_size)) +return ;break ;case SOF_CTRL_TYPE_DATA_GET :case SOF_CTRL_TYPE_DATA_SET : - expected_size += cdata->num_elems +sizeof (struct sof_abi_hdr ); +if (check_add_overflow ((size_t )cdata->num_elems, +sizeof (struct sof_abi_hdr ), + &expected_size)) +return ; +if (check_add_overflow (expected_size, +sizeof (struct sof_ipc_ctrl_data ), + &expected_size)) +return ;break ;default :return ;
Signature · 1 line
2.54.0
Peter Ujfalusi
maintainerPATCH 4/6 ASoC: SOF: ipc3-control: Validate size in snd_sof_update_control
In reply to Peter UjfalusiIn snd_sof_update_control(), firmware-provided cdata->num_elems is checked against local_cdata->data->size but never against the actual allocation size. If local_cdata->data->size was previously set to an inconsistent value, the memcpy could write past the allocated buffer.
Add a bounds check to ensure num_elems fits within the available space in the ipc_control_data allocation before copying.
--- sound/soc/sof/ipc3-control.c | 9 +++++++++ 1 file changed, 9 insertions(+)
sound/soc/sof/ipc3-control.c +9 -0
@@ -535,6 +535,15 @@ static void snd_sof_update_control(struct snd_sof_control *scontrol,return ; } +/* Verify the size fits within the allocation */ +if (cdata->num_elems > scontrol->max_size -sizeof (*local_cdata) - +sizeof (*local_cdata->data)) { +dev_err (scomp->dev, +"cdata binary size %u exceeds buffer\n" , + cdata->num_elems); +return ; + } +/* copy the new binary data */ memcpy (local_cdata->data, cdata->data, cdata->num_elems); }else if (cdata->num_elems != scontrol->num_channels) {
Signature · 1 line
2.54.0
Peter Ujfalusi
maintainerPATCH 5/6 ASoC: SOF: ipc3-control: Fix TOCTOU in bytes_put and bytes_get
In reply to Peter UjfalusiIn sof_ipc3_bytes_put(), the size used for the memcpy is derived from the old data->size already in the buffer, not the incoming new data's size field. If the new data has a different size, the copy length is
wrong: it may truncate valid data or copy stale bytes.
Similarly, sof_ipc3_bytes_get() checks data->size against max_size without accounting for the sizeof(struct sof_ipc_ctrl_data) offset of the flex array within the allocation.
Fix bytes_put to validate and use the incoming data's sof_abi_hdr.size from ucontrol before copying. Fix bytes_get to subtract sizeof(*cdata) from the bounds check to match the actual available space.
--- sound/soc/sof/ipc3-control.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
sound/soc/sof/ipc3-control.c +16 -7
@@ -315,10 +315,13 @@ static int sof_ipc3_bytes_get(struct snd_sof_control *scontrol, }/* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */ -if (data->size > scontrol->max_size -sizeof (*data)) { +if (data->size > scontrol->max_size -sizeof (*cdata) - +sizeof (*data)) {dev_err_ratelimited (scomp->dev,"%u bytes of control data is invalid, max is %zu\n" , - data->size, scontrol->max_size -sizeof (*data)); + data->size, + scontrol->max_size -sizeof (*cdata) - +sizeof (*data));return -EINVAL ; }
@@ -336,6 +339,8 @@ static int sof_ipc3_bytes_put(struct snd_sof_control *scontrol,struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;struct snd_soc_component *scomp = scontrol->scomp;struct sof_abi_hdr *data = cdata->data; +const struct sof_abi_hdr *new_hdr = + (const struct sof_abi_hdr *)ucontrol->value.bytes.data;size_t size;if (scontrol->max_size >sizeof (ucontrol->value.bytes.data)) {
@@ -344,14 +349,18 @@ static int sof_ipc3_bytes_put(struct snd_sof_control *scontrol,return -EINVAL ; } -/* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */ -if (data->size > scontrol->max_size -sizeof (*data)) { -dev_err_ratelimited (scomp->dev,"data size too big %u bytes max is %zu\n" , - data->size, scontrol->max_size -sizeof (*data)); +/* Validate the new data's size, not the old one */ +if (new_hdr->size > scontrol->max_size -sizeof (*cdata) - +sizeof (*new_hdr)) { +dev_err_ratelimited (scomp->dev, +"data size too big %u bytes max is %zu\n" , + new_hdr->size, + scontrol->max_size -sizeof (*cdata) - +sizeof (*new_hdr));return -EINVAL ; } - size = data->size +sizeof (*data); + size = new_hdr->size +sizeof (*new_hdr);/* copy from kcontrol */ memcpy (data, ucontrol->value.bytes.data, size);
Signature · 1 line
2.54.0
Peter Ujfalusi
maintainerPATCH 6/6 ASoC: SOF: ipc3-control: Fix heap overflow in bytes_ext put/get
In reply to Peter UjfalusiThe ipc_control_data buffer is allocated as kzalloc(max_size), where max_size covers the entire struct sof_ipc_ctrl_data including its flexible array payload. However, the bounds checks in bytes_ext_put and _bytes_ext_get compared user data lengths against max_size directly, ignoring that cdata->data sits at an offset of sizeof(struct sof_ipc_ctrl_data) bytes into the allocation.
This allowed writing up to sizeof(struct sof_ipc_ctrl_data) bytes past the end of the heap buffer from unprivileged userspace via the ALSA TLV kcontrol interface, and similarly allowed over-reading adjacent heap data on the get path.
Fix all bounds checks to subtract sizeof(*cdata) from max_size so they reflect the actual space available at the cdata->data offset. Also fix the error-path restore in bytes_ext_put which wrote to cdata->data instead of cdata, causing the same overflow.
--- sound/soc/sof/ipc3-control.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
sound/soc/sof/ipc3-control.c +19 -8
@@ -398,9 +398,17 @@ static int sof_ipc3_bytes_ext_put(struct snd_sof_control *scontrol, }/* be->max is coming from topology */ -if (header.length > scontrol->max_size) { -dev_err_ratelimited (scomp->dev,"Bytes data size %d exceeds max %zu\n" , - header.length, scontrol->max_size); +if (header.length > scontrol->max_size -sizeof (*cdata)) { +dev_err_ratelimited (scomp->dev,"Bytes data size %u exceeds max %zu\n" , + header.length, scontrol->max_size -sizeof (*cdata)); +return -EINVAL ; + } + +/* Ensure the data is large enough to contain the ABI header */ +if (header.length <sizeof (struct sof_abi_hdr )) { +dev_err_ratelimited (scomp->dev, +"Bytes data size %u less than ABI header %zu\n" , + header.length,sizeof (struct sof_abi_hdr ));return -EINVAL ; }
@@ -436,7 +444,7 @@ static int sof_ipc3_bytes_ext_put(struct snd_sof_control *scontrol, }/* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */ -if (cdata->data->size > scontrol->max_size -sizeof (struct sof_abi_hdr )) { +if (cdata->data->size > scontrol->max_size -sizeof (*cdata) -sizeof (struct sof_abi_hdr )) {dev_err_ratelimited (scomp->dev,"Mismatch in ABI data size (truncated?)\n" ); goto err_restore; }
@@ -452,7 +460,7 @@ static int sof_ipc3_bytes_ext_put(struct snd_sof_control *scontrol, err_restore:/* If we have an issue, we restore the old, valid bytes control data */ if (scontrol->old_ipc_control_data) { -memcpy (cdata->data, scontrol->old_ipc_control_data, scontrol->max_size); +memcpy (cdata, scontrol->old_ipc_control_data, scontrol->max_size);kfree (scontrol->old_ipc_control_data); scontrol->old_ipc_control_data =NULL ; }
@@ -491,10 +499,13 @@ static int _sof_ipc3_bytes_ext_get(struct snd_sof_control *scontrol, }/* check data size doesn't exceed max coming from topology */ -if (cdata->data->size > scontrol->max_size -sizeof (struct sof_abi_hdr )) { -dev_err_ratelimited (scomp->dev,"User data size %d exceeds max size %zu\n" , +if (cdata->data->size > scontrol->max_size -sizeof (*cdata) - +sizeof (struct sof_abi_hdr )) { +dev_err_ratelimited (scomp->dev, +"User data size %u exceeds max size %zu\n" , cdata->data->size, - scontrol->max_size -sizeof (struct sof_abi_hdr )); + scontrol->max_size -sizeof (*cdata) - +sizeof (struct sof_abi_hdr ));return -EINVAL ; }
Signature · 1 line
2.54.0
Mark Brown
maintainerQuoted 9 lines · Peter Ujfalusi
ASoC: SOF: ipc3/ipc4-control: harden kcontrol payload handling
Hi,
This series hardens SOF kcontrol data paths for both IPC3 and IPC4 by fixing size-handling bugs in put/get/update flows and tightening bounds checks around
firmware/user-providedpayload lengths.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.2
Thanks!
[1/6] ASoC: SOF: ipc4-control: Fix TOCTOU in sof_ipc4_bytes_put
https://git.kernel.org/broonie/sound/c/3ad673e7139c [2/6] ASoC: SOF: ipc4-control: Validate notification payload size https://git.kernel.org/broonie/sound/c/5bdfeccb7fbf [3/6] ASoC: SOF: ipc3-control: Use overflow checks in control_update size calc https://git.kernel.org/broonie/sound/c/8791977d7289 [4/6] ASoC: SOF: ipc3-control: Validate size in snd_sof_update_control https://git.kernel.org/broonie/sound/c/390aa4c9339b [5/6] ASoC: SOF: ipc3-control: Fix TOCTOU in bytes_put and bytes_get https://git.kernel.org/broonie/sound/c/1f97760417b5 [6/6] ASoC: SOF: ipc3-control: Fix heap overflow in bytes_ext put/get https://git.kernel.org/broonie/sound/c/fd46668d5389
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark