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/ipc/ipc4/handler-user.c b/src/ipc/ipc4/handler-user.c index 5e210bcf246b..21cbee59f9df 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; } diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index e8c170f43290..573029798f6e 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -230,6 +230,17 @@ 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 whole IPC4_FW_CONFIG reply built by basefw_config(). */ +#define BASEFW_CONFIG_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) + \ + sizeof(struct sof_tlv) + SOF_CONFIG_SIZE_MAX + \ + 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) { @@ -299,6 +310,14 @@ __cold static int basefw_config(uint32_t *data_offset, char *data) 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 worst-case reply does not fit. + */ + if (MIN(*data_offset, SOF_IPC_MSG_MAX_SIZE) < BASEFW_CONFIG_SIZE_MAX) + return IPC4_OUT_OF_MEMORY; + tlv_value_set(tuple, IPC4_FW_VERSION_FW_CFG, sizeof(version), version); tuple = tlv_next(tuple);