From: Abel Vesa <abel.vesa@oss.qualcomm.com>
To: Bjorn Andersson <andersson@kernel.org>,
Konrad Dybcio <konradybcio@kernel.org>,
Abel Vesa <abelvesa@kernel.org>, Andy Gross <agross@kernel.org>,
Jeffrey Hugo <jeffrey.l.hugo@gmail.com>
Cc: Jeff Hugo <jeff.hugo@oss.qualcomm.com>,
linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org,
Abel Vesa <abel.vesa@oss.qualcomm.com>,
Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit
Date: Sun, 06 Dec 2026 00:26:07 +0200 [thread overview]
Message-ID: <20261206-mdt-loader-overflow-v1-1-9bf8e7781247@oss.qualcomm.com> (raw)
In-Reply-To: <20261206-mdt-loader-overflow-v1-0-9bf8e7781247@oss.qualcomm.com>
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
next prev parent reply other threads:[~2026-12-05 22:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-05 22:26 ` [PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware Abel Vesa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261206-mdt-loader-overflow-v1-1-9bf8e7781247@oss.qualcomm.com \
--to=abel.vesa@oss.qualcomm.com \
--cc=abelvesa@kernel.org \
--cc=agross@kernel.org \
--cc=andersson@kernel.org \
--cc=jeff.hugo@oss.qualcomm.com \
--cc=jeffrey.l.hugo@gmail.com \
--cc=konradybcio@kernel.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sashiko-bot@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®