From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-arm@nongnu.org, qemu-devel@nongnu.org
Cc: Beniamino Galvani <b.galvani@gmail.com>,
Jason Wang <jasowang@redhat.com>,
Niek Linnenbank <nieklinnenbank@gmail.com>
Subject: [PATCH] hw/net/allwinner-sun8i-emac.c: Fix REG_ADDR_HIGH/LOW reads
Date: Tue, 24 Mar 2020 21:21:03 +0000 [thread overview]
Message-ID: <20200324212103.7616-1-peter.maydell@linaro.org> (raw)
Coverity points out (CID 1421926) that the read code for
REG_ADDR_HIGH reads off the end of the buffer, because it does a
32-bit read from byte 4 of a 6-byte buffer.
The code also has an endianness issue for both REG_ADDR_HIGH and
REG_ADDR_LOW, because it will do the wrong thing on a big-endian
host.
Rewrite the read code to use ldl_le_p() and lduw_le_p() to fix this;
the write code is not incorrect, but for consistency we make it use
stl_le_p() and stw_le_p().
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/net/allwinner-sun8i-emac.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/hw/net/allwinner-sun8i-emac.c b/hw/net/allwinner-sun8i-emac.c
index 3fc5e346401..fc67a1be70a 100644
--- a/hw/net/allwinner-sun8i-emac.c
+++ b/hw/net/allwinner-sun8i-emac.c
@@ -611,10 +611,10 @@ static uint64_t allwinner_sun8i_emac_read(void *opaque, hwaddr offset,
value = s->mii_data;
break;
case REG_ADDR_HIGH: /* MAC Address High */
- value = *(((uint32_t *) (s->conf.macaddr.a)) + 1);
+ value = lduw_le_p(s->conf.macaddr.a + 4);
break;
case REG_ADDR_LOW: /* MAC Address Low */
- value = *(uint32_t *) (s->conf.macaddr.a);
+ value = ldl_le_p(s->conf.macaddr.a);
break;
case REG_TX_DMA_STA: /* Transmit DMA Status */
break;
@@ -728,14 +728,10 @@ static void allwinner_sun8i_emac_write(void *opaque, hwaddr offset,
s->mii_data = value;
break;
case REG_ADDR_HIGH: /* MAC Address High */
- s->conf.macaddr.a[4] = (value & 0xff);
- s->conf.macaddr.a[5] = (value & 0xff00) >> 8;
+ stw_le_p(s->conf.macaddr.a + 4, value);
break;
case REG_ADDR_LOW: /* MAC Address Low */
- s->conf.macaddr.a[0] = (value & 0xff);
- s->conf.macaddr.a[1] = (value & 0xff00) >> 8;
- s->conf.macaddr.a[2] = (value & 0xff0000) >> 16;
- s->conf.macaddr.a[3] = (value & 0xff000000) >> 24;
+ stl_le_p(s->conf.macaddr.a, value);
break;
case REG_TX_DMA_STA: /* Transmit DMA Status */
case REG_TX_CUR_DESC: /* Transmit Current Descriptor */
--
2.20.1
next reply other threads:[~2020-03-24 21:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-24 21:21 Peter Maydell [this message]
2020-03-25 15:17 ` [PATCH] hw/net/allwinner-sun8i-emac.c: Fix REG_ADDR_HIGH/LOW reads Richard Henderson
2020-03-25 21:03 ` Niek Linnenbank
2020-03-25 21:11 ` Peter Maydell
2020-03-27 2:00 ` Jason Wang
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=20200324212103.7616-1-peter.maydell@linaro.org \
--to=peter.maydell@linaro.org \
--cc=b.galvani@gmail.com \
--cc=jasowang@redhat.com \
--cc=nieklinnenbank@gmail.com \
--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).