From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Fabiano Rosas" <farosas@suse.de>,
"Laurent Vivier" <lvivier@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
qemu-arm@nongnu.org (open list:Raspberry Pi)
Subject: [RFC PATCH] hw/dma: don't allow weird transfer lengths for bcm2835
Date: Tue, 11 Nov 2025 10:54:29 +0000 [thread overview]
Message-ID: <20251111105429.3993300-1-alex.bennee@linaro.org> (raw)
The datasheet doesn't explicitly say that TXFR_LEN has to be word
aligned but the fact there is a DMA_D_WIDTH flag to select between 32
bit and 128 bit strongly implies that is how it works. The downstream
rpi kernel also goes to efforts to not write sub-4 byte lengths so
lets:
- fail when mis-programmed and report GUEST_ERROR
- catch setting D_WIDTH for 128 bit and report UNIMP
- add comments that the DEBUG register isn't a straight write
This includes the test case from the reported bug which is of unknown
provenience but isn't particularly novel.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3201
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
hw/dma/bcm2835_dma.c | 19 +++++++++++++++++++
tests/qtest/bcm2835-dma-test.c | 22 ++++++++++++++++++++++
2 files changed, 41 insertions(+)
diff --git a/hw/dma/bcm2835_dma.c b/hw/dma/bcm2835_dma.c
index a2771ddcb52..e03247796cf 100644
--- a/hw/dma/bcm2835_dma.c
+++ b/hw/dma/bcm2835_dma.c
@@ -86,6 +86,19 @@ static void bcm2835_dma_update(BCM2835DMAState *s, unsigned c)
}
xlen_td = xlen;
+ if (ch->ti & BCM2708_DMA_D_WIDTH) {
+ qemu_log_mask(LOG_UNIMP, "%s: 128bit transfers not yet supported", __func__);
+ ch->cs |= BCM2708_DMA_ERR;
+ break;
+ }
+
+ /* datasheet implies 32bit or 128bit transfers only */
+ if (xlen & 0x3) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: bad transfer size\n", __func__);
+ ch->cs |= BCM2708_DMA_ERR;
+ break;
+ }
+
while (ylen != 0) {
/* Normal transfer mode */
while (xlen != 0) {
@@ -229,6 +242,12 @@ static void bcm2835_dma_write(BCM2835DMAState *s, hwaddr offset,
ch->conblk_ad = value;
break;
case BCM2708_DMA_DEBUG:
+ /* this needs masking
+ * 31:29 - WAZRI
+ * 28:04 - RO
+ * 03 - WAZRI
+ * 00:02 - W1CLR
+ */
ch->debug = value;
break;
default:
diff --git a/tests/qtest/bcm2835-dma-test.c b/tests/qtest/bcm2835-dma-test.c
index 18901b76d21..4b100480f25 100644
--- a/tests/qtest/bcm2835-dma-test.c
+++ b/tests/qtest/bcm2835-dma-test.c
@@ -105,12 +105,34 @@ static void bcm2835_dma_test_interrupts(void)
bcm2835_dma_test_interrupt(14, 11);
}
+static void test_cve_underflow_txfr_len_1(void)
+{
+ uint64_t dma_base = RASPI3_DMA_BASE; // 0x3f007000
+ uint32_t cb_addr = 0x1000;
+ uint32_t src_addr = 0x2000;
+ uint32_t dst_addr = 0x3000;
+ /* Prepare DMA Control Block with VULNERABLE configuration */
+ writel(cb_addr + 0, BCM2708_DMA_S_INC | BCM2708_DMA_D_INC); /* TI */
+ writel(cb_addr + 4, src_addr); /* source address */
+ writel(cb_addr + 8, dst_addr); /* destination address */
+ writel(cb_addr + 12, 1); /* ⚠️ txfr_len = 1 (TRIGGER!) */
+ writel(cb_addr + 16, 0); /* stride */
+ writel(cb_addr + 20, 0); /* next CB = NULL */
+ /* Set control block address */
+ writel(dma_base + BCM2708_DMA_ADDR, cb_addr);
+ /* Trigger DMA - this will cause the vulnerability */
+ writel(dma_base + BCM2708_DMA_CS, BCM2708_DMA_ACTIVE);
+ /* Without the fix, QEMU process will hang at 100% CPU */
+}
+
int main(int argc, char **argv)
{
int ret;
g_test_init(&argc, &argv, NULL);
qtest_add_func("/bcm2835/dma/test_interrupts",
bcm2835_dma_test_interrupts);
+ qtest_add_func("/bcm2835/dma/test_underflow_txfr",
+ test_cve_underflow_txfr_len_1);
qtest_start("-machine raspi3b");
ret = g_test_run();
qtest_end();
--
2.47.3
next reply other threads:[~2025-11-11 10:56 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-11 10:54 Alex Bennée [this message]
2025-11-11 11:24 ` [RFC PATCH] hw/dma: don't allow weird transfer lengths for bcm2835 Peter Maydell
2025-11-14 16:05 ` Yodel Eldar
2025-11-14 16:15 ` Peter Maydell
2025-11-14 16:43 ` Alex Bennée
2025-11-14 16:51 ` Florian Kauer
2025-11-16 13:48 ` Yodel Eldar
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=20251111105429.3993300-1-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=farosas@suse.de \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).