From b7e015132b9f9acf1a80c884e18cb59cabfd48d3 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Thu, 3 Dec 2026 11:09:10 +0000 Subject: [PATCH 1/2] ipc4: handler-user: avoid wrap in vendor config get bound check ipc4_get_vendor_config_module_instance() rejects a TL when produced_data + sizeof(struct sof_tlv) + max_length exceeds MAILBOX_DSPBOX_SIZE. max_length comes straight from the host and the sum is a 32-bit size_t on the DSP, so max_length = 0xffffffff wraps the sum to produced_data + 7 and the check always passes. Each getter is then told it has 0xffffffff bytes and the loop keeps appending replies past the end of ipc->comp_data (SOF_IPC_MSG_MAX_SIZE bytes). Found by the IPC4 fuzzer (ASan): 38 BaseFW TLs of {IPC4_FW_CONFIG, 0xffffffff} write at comp_data + 4096 on the 15th basefw_config() call. Check that the header fits first, then compare max_length against the space left instead of adding it. Signed-off-by: Tomasz Leman --- src/ipc/ipc4/handler-user.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ipc/ipc4/handler-user.c b/src/ipc/ipc4/handler-user.c index c6e40582b244..062f90dfa867 100644 --- a/src/ipc/ipc4/handler-user.c +++ b/src/ipc/ipc4/handler-user.c @@ -784,10 +784,14 @@ __cold int ipc4_get_vendor_config_module_instance(struct comp_dev *dev, struct sof_tlv *output_tlv = (struct sof_tlv *)(data_out + produced_data); /* the TLV header is written in front of the value, so it - * must fit in the DSPBOX together with max_length + * must fit in the DSPBOX together with max_length. + * max_length is host-controlled: compare it against the + * space left instead of adding it, as the sum wraps on + * 32-bit targets (e.g. max_length = 0xffffffff). */ - if (produced_data + sizeof(*output_tlv) + input_tl[i].max_length > - MAILBOX_DSPBOX_SIZE) { + if (produced_data + sizeof(*output_tlv) > MAILBOX_DSPBOX_SIZE || + input_tl[i].max_length > + MAILBOX_DSPBOX_SIZE - produced_data - sizeof(*output_tlv)) { tr_err(&ipc_tr, "error: response payload bigger than DSPBOX size"); return IPC4_FAILURE; } From 6a907c14ab09aea07105c8c1554f874467329f1c Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Thu, 3 Dec 2026 11:09:10 +0000 Subject: [PATCH 2/2] ipc4: base_fw: bound FW_CONFIG reply by the caller buffer size basefw_config() writes the whole IPC4_FW_CONFIG tuple list (about 280 bytes) without looking at *data_offset. Through a VENDOR_CONFIG_PARAM get, data points after earlier TLV replies inside ipc->comp_data and *data_offset is the host max_length, narrowed to the room that is left. A host sending many {IPC4_FW_CONFIG, 0} TLs passes the vendor loop bound on every TL while each reply is ~288 bytes, so the 15th reply runs past the end of the 4096 byte comp_data. Add BASEFW_CONFIG_FIXED_SIZE_MAX, the worst-case size of the fixed tuples including the vendor tuples (new BASEFW_VENDOR_FW_CONFIG_SIZE_MAX). Build the nested IPC4_FW_SOF_INFO tuple once up front, so that its actual size, not the all-codec-slots worst case, is added to the bound, and the checked size is the size that gets written. Refuse with IPC4_OUT_OF_MEMORY when MIN(*data_offset, SOF_IPC_MSG_MAX_SIZE) is smaller, as done for the extended perf data reply. Signed-off-by: Tomasz Leman --- src/include/ipc4/base_fw_vendor.h | 13 +++++++++ src/kernel/audio/base_fw.c | 44 ++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/include/ipc4/base_fw_vendor.h b/src/include/ipc4/base_fw_vendor.h index 98250eac9c3a..7a75f049e688 100644 --- a/src/include/ipc4/base_fw_vendor.h +++ b/src/include/ipc4/base_fw_vendor.h @@ -26,6 +26,13 @@ struct comp_dev; */ #ifdef CONFIG_IPC4_BASE_FW_INTEL +/** + * \brief Upper bound in bytes of the tuples basefw_vendor_fw_config() adds: + * three uint32_t value tuples, each with a type and a length word. + * \ingroup ipc4_base_fw_api + */ +#define BASEFW_VENDOR_FW_CONFIG_SIZE_MAX (3 * 3 * sizeof(uint32_t)) + /** * \brief Vendor specific routine to add data tuples to FW_CONFIG * structure sent to host via IPC. @@ -116,6 +123,12 @@ int basefw_vendor_dma_control(uint32_t node_id, #else /* !CONFIG_IPC4_BASE_FW_INTEL */ +/** + * \brief Upper bound in bytes of the tuples basefw_vendor_fw_config() adds. + * \ingroup ipc4_base_fw_api + */ +#define BASEFW_VENDOR_FW_CONFIG_SIZE_MAX 0 + static inline int basefw_vendor_fw_config(uint32_t *data_offset, char *data) { *data_offset = 0; diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index e8c170f43290..9d57076e71d1 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -230,6 +230,19 @@ static void get_codec_info(struct sof_tlv **tuple, bool include_empty) #define SOF_CONFIG_SIZE_MAX (SOF_CONFIG_MEMBER_SIZE(sof_ipc4_codec_info_data) + \ sizeof(struct sof_tlv) + sizeof(uint32_t)) +/* Upper bound of the uint32_t value tuples basefw_config() writes. */ +#define BASEFW_CONFIG_U32_TUPLES_MAX 17 + +/* Upper bound of the IPC4_FW_CONFIG reply built by basefw_config(), without + * the nested IPC4_FW_SOF_INFO tuple whose size depends on the active codecs. + */ +#define BASEFW_CONFIG_FIXED_SIZE_MAX (sizeof(struct sof_tlv) + 4 * sizeof(uint16_t) + \ + BASEFW_CONFIG_U32_TUPLES_MAX * \ + (sizeof(struct sof_tlv) + sizeof(uint32_t)) + \ + sizeof(struct sof_tlv) + \ + sizeof(struct ipc4_scheduler_config) + \ + BASEFW_VENDOR_FW_CONFIG_SIZE_MAX) + /* Serialize nested IPC4_FW_SOF_INFO TLVs (codec list + SOF capability mask). */ static uint32_t build_sof_info_tlvs(char *buf, bool include_empty_codec_info) { @@ -250,21 +263,6 @@ static uint32_t build_sof_info_tlvs(char *buf, bool include_empty_codec_info) return (uint32_t)((char *)sof_config_tuple - buf); } -/* Append IPC4_FW_SOF_INFO TLV to IPC4_FW_CONFIG response. */ -static void base_fw_sof_config(struct sof_tlv **tuple) -{ - char sof_config_data[SOF_CONFIG_SIZE_MAX] = { 0 }; - uint32_t sof_config_size; - - sof_config_size = build_sof_info_tlvs(sof_config_data, false); - if (sof_config_size == 0) - return; - - tlv_value_set(*tuple, IPC4_FW_SOF_INFO, sof_config_size, sof_config_data); - - *tuple = tlv_next(*tuple); -} - /* Handle IPC4_SOF_INFO_GET query with live codec and SOF capability TLVs. */ __cold static int basefw_sof_info_get(uint32_t *data_offset, char *data) { @@ -294,11 +292,24 @@ __cold static int basefw_config(uint32_t *data_offset, char *data) uint16_t version[4] = {SOF_MAJOR, SOF_MINOR, SOF_MICRO, SOF_BUILD}; struct sof_tlv *tuple = (struct sof_tlv *)data; struct ipc4_scheduler_config sche_cfg; + char sof_config_data[SOF_CONFIG_SIZE_MAX] = { 0 }; uint32_t plat_data_offset = 0; uint32_t log_bytes_size = 0; + uint32_t sof_config_size; assert_can_be_cold(); + /* data_offset is in/out: the host max reply length comes in, and the + * vendor config path narrows it to what is left of comp_data after + * earlier TLVs. The tuples below are written without further checks, + * so refuse up front when the reply does not fit. The SOF info tuple + * is built once here so the checked size is the size that is written. + */ + sof_config_size = build_sof_info_tlvs(sof_config_data, false); + if (MIN(*data_offset, SOF_IPC_MSG_MAX_SIZE) < + BASEFW_CONFIG_FIXED_SIZE_MAX + sizeof(struct sof_tlv) + sof_config_size) + return IPC4_OUT_OF_MEMORY; + tlv_value_set(tuple, IPC4_FW_VERSION_FW_CFG, sizeof(version), version); tuple = tlv_next(tuple); @@ -374,7 +385,8 @@ __cold static int basefw_config(uint32_t *data_offset, char *data) tuple = tlv_next(tuple); - base_fw_sof_config(&tuple); + tlv_value_set(tuple, IPC4_FW_SOF_INFO, sof_config_size, sof_config_data); + tuple = tlv_next(tuple); /* add platform specific tuples */ basefw_vendor_fw_config(&plat_data_offset, (char *)tuple);