[PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit

From: Abel Vesa

Date: Sat Dec 05 2026 - 17:26:24 EST


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@xxxxxxxxxx>
Closes: https://sashiko.dev/#/bug/linux-f20c72c2-eeec-496e-8e64-0d17d216e416
Signed-off-by: Abel Vesa <abel.vesa@xxxxxxxxxxxxxxxx>
---
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