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>
Subject: [PATCH 2/2] soc: qcom: mdt_loader: Validate metadata sizes against the firmware
Date: Sun, 06 Dec 2026 00:26:08 +0200 [thread overview]
Message-ID: <20261206-mdt-loader-overflow-v1-2-9bf8e7781247@oss.qualcomm.com> (raw)
In-Reply-To: <20261206-mdt-loader-overflow-v1-0-9bf8e7781247@oss.qualcomm.com>
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
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 ` [PATCH 1/2] soc: qcom: mdt_loader: Fix segment bounds check overflow on 32-bit Abel Vesa
2026-12-05 22:26 ` Abel Vesa [this message]
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-2-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 \
/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®