hw/net/can/ctucan: fix Coverity and other issues
v3 cover hw/net/can/ctucan: fix Coverity and other issues
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(-)
Signature · 1 line
2.20.1
PATCH v3 1/4 hw/net/can/ctucan: Don't allow guest to write off end of tx_buffer
From: Peter Maydell <[email protected]>
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.
--- hw/net/can/ctucan_core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
hw/net/can/ctucan_core.c +4 -2
@@ -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); }
Signature · 1 line
2.20.1
PATCH v3 2/4 hw/net/can/ctucan: Avoid unused value in ctucan_send_ready_buffers()
In reply to Pavel PisaFrom: Peter Maydell <[email protected]>
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().
--- hw/net/can/ctucan_core.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
hw/net/can/ctucan_core.c +3 -12
@@ -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 ); }
Signature · 1 line
2.20.1
PATCH v3 3/4 hw/net/can/ctucan_core: Handle big-endian hosts
In reply to Pavel PisaFrom: Peter Maydell <[email protected]>
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.
--- hw/net/can/ctucan_core.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
hw/net/can/ctucan_core.h +1 -2
@@ -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
Signature · 1 line
2.20.1
PATCH v3 4/4 hw/net/can/ctucan_core: Use stl_le_p to write to tx_buffers
In reply to Pavel PisaFrom: Peter Maydell <[email protected]>
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.
--- hw/net/can/ctucan_core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
hw/net/can/ctucan_core.c +2 -4
@@ -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 ) {
Signature · 1 line
2.20.1
Jason Wang
AppliedQuoted 29 lines · Pavel Pisa
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 thetx_bufferarrays
* we had an unused value inctucan_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 usestl_le_p()rather than casting uint_t* touint32_t*Tested with "make check" only.
Changes
v1->v2: don'tassert()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 onx86_64guest onx86_64host and bige-edian MIPS guest onx86_64host Pavel Pisa.Peter Maydell (4):
hw/net/can/ctucan: Don't allow guest to write off end oftx_bufferhw/net/can/ctucan: Avoid unused value inctucan_send_ready_buffers()hw/net/can/ctucan_core: Handle big-endian hosts hw/net/can/ctucan_core: Usestl_le_pto write totx_buffers
Quoted 3 lines
hw/net/can/ctucan_core.c | 23 +++++++---------------- hw/net/can/ctucan_core.h | 3 +-- 2 files changed, 8 insertions(+), 18 deletions(-)
Applied.
Thanks