[PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware
From: Abel Vesa
Date: Sat Dec 05 2026 - 17:26:28 EST
qcom_mdt_read_metadata() copies the ELF header out of the loaded
firmware, using the p_filesz of the first program header as its size,
without checking that the firmware is actually that large. A bogus
p_filesz thus makes the memcpy() read beyond the end of the firmware
buffer.
On 32-bit platforms the size of the metadata buffer, ehdr_size +
hash_size, can also wrap around, resulting in a buffer too small for
the data copied into it. The same goes for the p_offset + hash_size
check deciding whether the hash segment is part of the loaded file,
which can let the memcpy() of the hash segment read out of bounds.
Reject ELF headers that are larger than the firmware, use size_add()
when sizing the metadata buffer and check the hash segment bounds in
64 bits.
Fixes: 498b98e93900 ("soc: qcom: mdt_loader: Support loading non-split images")
Signed-off-by: Abel Vesa <abel.vesa@xxxxxxxxxxxxxxxx>
---
drivers/soc/qcom/mdt_loader.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/soc/qcom/mdt_loader.c b/drivers/soc/qcom/mdt_loader.c
index 0009e9a03e17..b103379a4d12 100644
--- a/drivers/soc/qcom/mdt_loader.c
+++ b/drivers/soc/qcom/mdt_loader.c
@@ -201,7 +201,11 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
ehdr_size = phdrs[0].p_filesz;
hash_size = phdrs[hash_segment].p_filesz;
- data = kmalloc(ehdr_size + hash_size, GFP_KERNEL);
+ /* The ELF header is always copied from the loaded file */
+ if (ehdr_size > fw->size)
+ return ERR_PTR(-EINVAL);
+
+ data = kmalloc(size_add(ehdr_size, hash_size), GFP_KERNEL);
if (!data)
return ERR_PTR(-ENOMEM);
@@ -212,7 +216,7 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
/* Firmware is split and hash is packed following the ELF header */
hash_offset = phdrs[0].p_filesz;
memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
- } else if (phdrs[hash_segment].p_offset + hash_size <= fw->size) {
+ } else if ((u64)phdrs[hash_segment].p_offset + hash_size <= fw->size) {
/* Hash is in its own segment, but within the loaded file */
hash_offset = phdrs[hash_segment].p_offset;
memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
--
2.54.0