* [PATCH 0/2] soc: qcom: mdt_loader: Fix integer overflows in segment bounds checks
@ 2026-12-05 22:26 Abel Vesa
2026-12-05 22:26 ` [PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit Abel Vesa
2026-12-05 22:26 ` [PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware Abel Vesa
0 siblings, 2 replies; 3+ messages in thread
From: Abel Vesa @ 2026-12-05 22:26 UTC (permalink / raw)
To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Andy Gross, Jeffrey Hugo
Cc: Jeff Hugo, linux-arm-msm, linux-kernel, Abel Vesa, Sashiko
Sashiko reported that the segment bounds check in
qcom_mdt_load_no_init() can be bypassed on 32-bit platforms, where
offset + p_memsz is computed in 32 bits and can wrap around, letting a
crafted firmware image zero memory beyond the end of the memory region.
Patch 1 fixes that by doing the check in 64 bits, which also covers the
offset getting truncated with LPAE.
While looking at it, qcom_mdt_read_metadata() turned out to have similar
issues: it copies the ELF header using a size taken from the image
without checking it against the size of the firmware, which reads out
of bounds on any platform, and its other size computations can wrap on
32-bit. Patch 2 fixes these.
Prepared with help from an LLM-based assistant.
Signed-off-by: Abel Vesa <abel.vesa@oss.qualcomm.com>
---
Abel Vesa (2):
soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit
soc: qcom: mdt_loader: Validate metadata sizes against the firmware
drivers/soc/qcom/mdt_loader.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
---
base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
change-id: 20261205-mdt-loader-overflow-fa009fc16041
Best regards,
--
Abel
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit
2026-12-05 22:26 [PATCH 0/2] soc: qcom: mdt_loader: Fix integer overflows in segment bounds checks Abel Vesa
@ 2026-12-05 22:26 ` Abel Vesa
2026-12-05 22:26 ` [PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware Abel Vesa
1 sibling, 0 replies; 3+ messages in thread
From: Abel Vesa @ 2026-12-05 22:26 UTC (permalink / raw)
To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Andy Gross, Jeffrey Hugo
Cc: Jeff Hugo, linux-arm-msm, linux-kernel, Abel Vesa, Sashiko
qcom_mdt_load_no_init() makes sure that each loadable segment fits in
the memory region by checking:
offset + phdr->p_memsz > mem_size
On 32-bit platforms, offset is a 32-bit ssize_t and p_memsz is a u32,
so the sum is computed in 32 bits and can wrap around. A segment with a
large enough p_memsz then passes the check, and the memset() that clears
the part of the segment not backed by the file writes far beyond the end
of the memory region.
With LPAE, the offset itself is also truncated, as it is computed from
a 64-bit phys_addr_t, so a segment below the memory region can even
pass the offset < 0 check.
Do the bounds check in 64 bits, where neither can happen, and only then
compute the offset into the memory region.
Fixes: 7f0dd07a9b29 ("remoteproc: qcom: mdt_loader: Refactor MDT loader")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-f20c72c2-eeec-496e-8e64-0d17d216e416
Signed-off-by: Abel Vesa <abel.vesa@oss.qualcomm.com>
---
drivers/soc/qcom/mdt_loader.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/drivers/soc/qcom/mdt_loader.c b/drivers/soc/qcom/mdt_loader.c
index 0d55e9e0af3f..0009e9a03e17 100644
--- a/drivers/soc/qcom/mdt_loader.c
+++ b/drivers/soc/qcom/mdt_loader.c
@@ -346,7 +346,7 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
const struct elf32_hdr *ehdr;
phys_addr_t mem_reloc;
phys_addr_t min_addr = PHYS_ADDR_MAX;
- ssize_t offset;
+ size_t offset;
bool relocate = false;
bool is_split;
void *ptr;
@@ -396,13 +396,19 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
if (!mdt_phdr_loadable(phdr))
continue;
- offset = phdr->p_paddr - mem_reloc;
- if (offset < 0 || offset + phdr->p_memsz > mem_size) {
+ /*
+ * Do the math in 64 bits, as the offset and the end of the
+ * segment might not fit in a size_t on 32-bit platforms.
+ */
+ if (phdr->p_paddr < mem_reloc ||
+ (u64)phdr->p_paddr - mem_reloc + phdr->p_memsz > mem_size) {
dev_err(dev, "segment outside memory range\n");
ret = -EINVAL;
break;
}
+ offset = phdr->p_paddr - mem_reloc;
+
if (phdr->p_filesz > phdr->p_memsz) {
dev_err(dev,
"refusing to load segment %d with p_filesz > p_memsz\n",
--
2.54.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware
2026-12-05 22:26 [PATCH 0/2] soc: qcom: mdt_loader: Fix integer overflows in segment bounds checks Abel Vesa
2026-12-05 22:26 ` [PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit Abel Vesa
@ 2026-12-05 22:26 ` Abel Vesa
1 sibling, 0 replies; 3+ messages in thread
From: Abel Vesa @ 2026-12-05 22:26 UTC (permalink / raw)
To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Andy Gross, Jeffrey Hugo
Cc: Jeff Hugo, linux-arm-msm, linux-kernel, Abel Vesa
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@oss.qualcomm.com>
---
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-05 22:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05 22:26 [PATCH 0/2] soc: qcom: mdt_loader: Fix integer overflows in segment bounds checks Abel Vesa
2026-12-05 22:26 ` [PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit Abel Vesa
2026-12-05 22:26 ` [PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware Abel Vesa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®