All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues
@ 2020-11-10 21:52 Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 1/4] hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer Pavel Pisa
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Pavel Pisa @ 2020-11-10 21:52 UTC (permalink / raw)
  To: qemu-devel, Peter Maydell
  Cc: Pavel Pisa, Jason Wang, Vikram Garhwal, Ondrej Ille, Jan Charvát

Credit for finding and fixes goes to Peter Maydell

This patchset fixes a couple of issues spotted by Coverity:
 * incorrect address checks meant the guest could write off the
   end of the tx_buffer arrays
 * we had an unused value in ctucan_send_ready_buffers()
and also some I noticed while reading the code:
 * we don't adjust the device's non-portable use of bitfields
   on bigendian hosts
 * we should use stl_le_p() rather than casting uint_t* to
   uint32_t*

Tested with "make check" only.

Changes v1->v2: don't assert() the can't-happen case in patch 1,
to allow for future adjustment of #defines that correspond to
h/w synthesis parameters.

Changes v2->v3: minnor corrections of range checking,
support for unaligned and partial word writes into Tx
buffers. Tested on x86_64 guest on x86_64 host and bige-edian
MIPS guest on x86_64 host Pavel Pisa.

Peter Maydell (4):
  hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer
  hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers()
  hw/net/can/ctucan_core: Handle big-endian hosts
  hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers

 hw/net/can/ctucan_core.c | 23 +++++++----------------
 hw/net/can/ctucan_core.h |  3 +--
 2 files changed, 8 insertions(+), 18 deletions(-)

-- 
2.20.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH for-5.2 v3 1/4] hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer
  2020-11-10 21:52 [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Pavel Pisa
@ 2020-11-10 21:52 ` Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 2/4] hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers() Pavel Pisa
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Pavel Pisa @ 2020-11-10 21:52 UTC (permalink / raw)
  To: qemu-devel, Peter Maydell
  Cc: Pavel Pisa, Jason Wang, Vikram Garhwal, Ondrej Ille, Jan Charvát

From: Peter Maydell <peter.maydell@linaro.org>

The ctucan device has 4 CAN bus cores, each of which has a set of 20
32-bit registers for writing the transmitted data. The registers are
however not contiguous; each core's buffers is 0x100 bytes after
the last.

We got the checks on the address wrong in the ctucan_mem_write()
function:
 * the first "is addr in range at all" check allowed
   addr == CTUCAN_CORE_MEM_SIZE, which is actually the first
   byte off the end of the range
 * the decode of addresses into core-number plus offset in the
   tx buffer for that core failed to check that the offset was
   in range, so the guest could write off the end of the
   tx_buffer[] array

NB: currently the values of CTUCAN_CORE_MEM_SIZE, CTUCAN_CORE_TXBUF_NUM,
etc, make "buff_num >= CTUCAN_CORE_TXBUF_NUM" impossible, but we
retain this as a runtime check rather than an assertion to permit
those values to be changed in future (in hardware they are
configurable synthesis parameters).

Fix the top level check, and check the offset is within the buffer.

Fixes: Coverity CID 1432874
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Tested-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
---
 hw/net/can/ctucan_core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/hw/net/can/ctucan_core.c b/hw/net/can/ctucan_core.c
index d20835cd7e..8486f429d7 100644
--- a/hw/net/can/ctucan_core.c
+++ b/hw/net/can/ctucan_core.c
@@ -303,7 +303,7 @@ void ctucan_mem_write(CtuCanCoreState *s, hwaddr addr, uint64_t val,
     DPRINTF("write 0x%02llx addr 0x%02x\n",
             (unsigned long long)val, (unsigned int)addr);
 
-    if (addr > CTUCAN_CORE_MEM_SIZE) {
+    if (addr >= CTUCAN_CORE_MEM_SIZE) {
         return;
     }
 
@@ -312,7 +312,9 @@ void ctucan_mem_write(CtuCanCoreState *s, hwaddr addr, uint64_t val,
         addr -= CTU_CAN_FD_TXTB1_DATA_1;
         buff_num = addr / CTUCAN_CORE_TXBUFF_SPAN;
         addr %= CTUCAN_CORE_TXBUFF_SPAN;
-        if (buff_num < CTUCAN_CORE_TXBUF_NUM) {
+        addr &= ~3;
+        if ((buff_num < CTUCAN_CORE_TXBUF_NUM) &&
+            (addr < sizeof(s->tx_buffer[buff_num].data))) {
             uint32_t *bufp = (uint32_t *)(s->tx_buffer[buff_num].data + addr);
             *bufp = cpu_to_le32(val);
         }
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH for-5.2 v3 2/4] hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers()
  2020-11-10 21:52 [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 1/4] hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer Pavel Pisa
@ 2020-11-10 21:52 ` Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 3/4] hw/net/can/ctucan_core: Handle big-endian hosts Pavel Pisa
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Pavel Pisa @ 2020-11-10 21:52 UTC (permalink / raw)
  To: qemu-devel, Peter Maydell
  Cc: Pavel Pisa, Jason Wang, Vikram Garhwal, Ondrej Ille, Jan Charvát

From: Peter Maydell <peter.maydell@linaro.org>

Coverity points out that in ctucan_send_ready_buffers() we
set buff_st_mask = 0xf << (i * 4) inside the loop, but then
we never use it before overwriting it later.

The only thing we use the mask for is as part of the code that is
inserting the new buff_st field into tx_status.  That is more
comprehensibly written using deposit32(), so do that and drop the
mask variable entirely.

We also update the buff_st local variable at multiple points
during this function, but nothing can ever see these
intermediate values, so just drop those, write the final
TXT_TOK as a fixed constant value, and collapse the only
remaining set/use of buff_st down into an extract32().

Fixes: Coverity CID 1432869
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Acked-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Tested-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
---
 hw/net/can/ctucan_core.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)

diff --git a/hw/net/can/ctucan_core.c b/hw/net/can/ctucan_core.c
index 8486f429d7..f49c76261c 100644
--- a/hw/net/can/ctucan_core.c
+++ b/hw/net/can/ctucan_core.c
@@ -240,8 +240,6 @@ static void ctucan_send_ready_buffers(CtuCanCoreState *s)
     uint8_t *pf;
     int buff2tx_idx;
     uint32_t tx_prio_max;
-    unsigned int buff_st;
-    uint32_t buff_st_mask;
 
     if (!s->mode_settings.s.ena) {
         return;
@@ -256,10 +254,7 @@ static void ctucan_send_ready_buffers(CtuCanCoreState *s)
         for (i = 0; i < CTUCAN_CORE_TXBUF_NUM; i++) {
             uint32_t prio;
 
-            buff_st_mask = 0xf << (i * 4);
-            buff_st = (s->tx_status.u32 >> (i * 4)) & 0xf;
-
-            if (buff_st != TXT_RDY) {
+            if (extract32(s->tx_status.u32, i * 4, 4) != TXT_RDY) {
                 continue;
             }
             prio = (s->tx_priority.u32 >> (i * 4)) & 0x7;
@@ -271,10 +266,7 @@ static void ctucan_send_ready_buffers(CtuCanCoreState *s)
         if (buff2tx_idx == -1) {
             break;
         }
-        buff_st_mask = 0xf << (buff2tx_idx * 4);
-        buff_st = (s->tx_status.u32 >> (buff2tx_idx * 4)) & 0xf;
         int_stat.u32 = 0;
-        buff_st = TXT_RDY;
         pf = s->tx_buffer[buff2tx_idx].data;
         ctucan_buff2frame(pf, &frame);
         s->status.s.idle = 0;
@@ -283,12 +275,11 @@ static void ctucan_send_ready_buffers(CtuCanCoreState *s)
         s->status.s.idle = 1;
         s->status.s.txs = 0;
         s->tx_fr_ctr.s.tx_fr_ctr_val++;
-        buff_st = TXT_TOK;
         int_stat.s.txi = 1;
         int_stat.s.txbhci = 1;
         s->int_stat.u32 |= int_stat.u32 & ~s->int_mask.u32;
-        s->tx_status.u32 = (s->tx_status.u32 & ~buff_st_mask) |
-                        (buff_st << (buff2tx_idx * 4));
+        s->tx_status.u32 = deposit32(s->tx_status.u32,
+                                     buff2tx_idx * 4, 4, TXT_TOK);
     } while (1);
 }
 
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH for-5.2 v3 3/4] hw/net/can/ctucan_core: Handle big-endian hosts
  2020-11-10 21:52 [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 1/4] hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 2/4] hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers() Pavel Pisa
@ 2020-11-10 21:52 ` Pavel Pisa
  2020-11-10 21:52 ` [PATCH for-5.2 v3 4/4] hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers Pavel Pisa
  2020-11-11 12:50 ` [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Jason Wang
  4 siblings, 0 replies; 6+ messages in thread
From: Pavel Pisa @ 2020-11-10 21:52 UTC (permalink / raw)
  To: qemu-devel, Peter Maydell
  Cc: Vikram Garhwal, Jason Wang, Philippe Mathieu-Daudé,
	Ondrej Ille, Jan Charvát, Pavel Pisa

From: Peter Maydell <peter.maydell@linaro.org>

The ctucan driver defines types for its registers which are a union
of a uint32_t with a struct with bitfields for the individual
fields within that register. This is a bad idea, because bitfields
aren't portable. The ctu_can_fd_regs.h header works around the
most glaring of the portability issues by defining the
fields in two different orders depending on the setting of the
__LITTLE_ENDIAN_BITFIELD define. However, in ctucan_core.h this
is unconditionally set to 1, which is wrong for big-endian hosts.

Set it only if HOST_WORDS_BIGENDIAN is not set. There is no need
for a "have we defined it already" guard, because the only place
that should set it is ctucan_core.h, which has the usual
double-inclusion guard.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Acked-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Tested-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
---
 hw/net/can/ctucan_core.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/hw/net/can/ctucan_core.h b/hw/net/can/ctucan_core.h
index f21cb1c5ec..bbc09ae067 100644
--- a/hw/net/can/ctucan_core.h
+++ b/hw/net/can/ctucan_core.h
@@ -31,8 +31,7 @@
 #include "exec/hwaddr.h"
 #include "net/can_emu.h"
 
-
-#ifndef __LITTLE_ENDIAN_BITFIELD
+#ifndef HOST_WORDS_BIGENDIAN
 #define __LITTLE_ENDIAN_BITFIELD 1
 #endif
 
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH for-5.2 v3 4/4] hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers
  2020-11-10 21:52 [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Pavel Pisa
                   ` (2 preceding siblings ...)
  2020-11-10 21:52 ` [PATCH for-5.2 v3 3/4] hw/net/can/ctucan_core: Handle big-endian hosts Pavel Pisa
@ 2020-11-10 21:52 ` Pavel Pisa
  2020-11-11 12:50 ` [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Jason Wang
  4 siblings, 0 replies; 6+ messages in thread
From: Pavel Pisa @ 2020-11-10 21:52 UTC (permalink / raw)
  To: qemu-devel, Peter Maydell
  Cc: Vikram Garhwal, Jason Wang, Philippe Mathieu-Daudé,
	Ondrej Ille, Jan Charvát, Pavel Pisa

From: Peter Maydell <peter.maydell@linaro.org>

Instead of casting an address within a uint8_t array to a
uint32_t*, use stl_le_p(). This handles possibly misaligned
addresses which would otherwise crash on some hosts.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Tested-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
---
 hw/net/can/ctucan_core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/hw/net/can/ctucan_core.c b/hw/net/can/ctucan_core.c
index f49c76261c..d171c372e0 100644
--- a/hw/net/can/ctucan_core.c
+++ b/hw/net/can/ctucan_core.c
@@ -303,11 +303,9 @@ void ctucan_mem_write(CtuCanCoreState *s, hwaddr addr, uint64_t val,
         addr -= CTU_CAN_FD_TXTB1_DATA_1;
         buff_num = addr / CTUCAN_CORE_TXBUFF_SPAN;
         addr %= CTUCAN_CORE_TXBUFF_SPAN;
-        addr &= ~3;
         if ((buff_num < CTUCAN_CORE_TXBUF_NUM) &&
-            (addr < sizeof(s->tx_buffer[buff_num].data))) {
-            uint32_t *bufp = (uint32_t *)(s->tx_buffer[buff_num].data + addr);
-            *bufp = cpu_to_le32(val);
+            ((addr + size) <= sizeof(s->tx_buffer[buff_num].data))) {
+            stn_le_p(s->tx_buffer[buff_num].data + addr, size, val);
         }
     } else {
         switch (addr & ~3) {
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues
  2020-11-10 21:52 [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Pavel Pisa
                   ` (3 preceding siblings ...)
  2020-11-10 21:52 ` [PATCH for-5.2 v3 4/4] hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers Pavel Pisa
@ 2020-11-11 12:50 ` Jason Wang
  4 siblings, 0 replies; 6+ messages in thread
From: Jason Wang @ 2020-11-11 12:50 UTC (permalink / raw)
  To: Pavel Pisa, qemu-devel, Peter Maydell
  Cc: Ondrej Ille, Vikram Garhwal, Jan Charvát


On 2020/11/11 上午5:52, Pavel Pisa wrote:
> Credit for finding and fixes goes to Peter Maydell
>
> This patchset fixes a couple of issues spotted by Coverity:
>   * incorrect address checks meant the guest could write off the
>     end of the tx_buffer arrays
>   * we had an unused value in ctucan_send_ready_buffers()
> and also some I noticed while reading the code:
>   * we don't adjust the device's non-portable use of bitfields
>     on bigendian hosts
>   * we should use stl_le_p() rather than casting uint_t* to
>     uint32_t*
>
> Tested with "make check" only.
>
> Changes v1->v2: don't assert() the can't-happen case in patch 1,
> to allow for future adjustment of #defines that correspond to
> h/w synthesis parameters.
>
> Changes v2->v3: minnor corrections of range checking,
> support for unaligned and partial word writes into Tx
> buffers. Tested on x86_64 guest on x86_64 host and bige-edian
> MIPS guest on x86_64 host Pavel Pisa.
>
> Peter Maydell (4):
>    hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer
>    hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers()
>    hw/net/can/ctucan_core: Handle big-endian hosts
>    hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers
>
>   hw/net/can/ctucan_core.c | 23 +++++++----------------
>   hw/net/can/ctucan_core.h |  3 +--
>   2 files changed, 8 insertions(+), 18 deletions(-)


Applied.

Thanks


>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-11-11 12:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-10 21:52 [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Pavel Pisa
2020-11-10 21:52 ` [PATCH for-5.2 v3 1/4] hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer Pavel Pisa
2020-11-10 21:52 ` [PATCH for-5.2 v3 2/4] hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers() Pavel Pisa
2020-11-10 21:52 ` [PATCH for-5.2 v3 3/4] hw/net/can/ctucan_core: Handle big-endian hosts Pavel Pisa
2020-11-10 21:52 ` [PATCH for-5.2 v3 4/4] hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers Pavel Pisa
2020-11-11 12:50 ` [PATCH for-5.2 v3 0/4] hw/net/can/ctucan: fix Coverity and other issues Jason Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.