* [PATCH] RDMA/cxgb3: Fix unintended sign extension
@ 2018-10-25 14:31 Colin King
2018-10-25 15:26 ` Steve Wise
2018-10-25 16:03 ` David Laight
0 siblings, 2 replies; 3+ messages in thread
From: Colin King @ 2018-10-25 14:31 UTC (permalink / raw)
To: Steve Wise, Doug Ledford, Jason Gunthorpe, linux-rdma
Cc: kernel-janitors, linux-kernel
From: Colin Ian King <colin.king@canonical.com>
In the expression "utx_len << 28", utx_len starts as u8, but is promoted
to a signed int, then sign-extended to u64. If utx_len is 0xf8 or greater
then the sign extension will set all the upper bits of utx_cmd which is
probably not what was intended. Cast to utx_len to u64 to avoid the sign
extension.
Detected by CoverityScan, CID#138764 ("Unintended sign extension")
Fixes: b038ced7b370 ("RDMA/cxgb3: Add driver for Chelsio T3 RNIC")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
drivers/infiniband/hw/cxgb3/cxio_hal.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/infiniband/hw/cxgb3/cxio_hal.c b/drivers/infiniband/hw/cxgb3/cxio_hal.c
index dcb4bba522ba..c5cb80ccc6d6 100644
--- a/drivers/infiniband/hw/cxgb3/cxio_hal.c
+++ b/drivers/infiniband/hw/cxgb3/cxio_hal.c
@@ -647,7 +647,7 @@ static int cxio_hal_ctrl_qp_write_mem(struct cxio_rdev *rdev_p, u32 addr,
wqe += (sizeof(struct t3_bypass_wr) >> 3);
utx_cmd = (T3_UTX_MEM_WRITE << 28) | (addr + i * 3);
utx_cmd <<= 32;
- utx_cmd |= (utx_len << 28) | ((utx_len << 2) + 1);
+ utx_cmd |= ((u64)utx_len << 28) | ((utx_len << 2) + 1);
*wqe = cpu_to_be64(utx_cmd);
wqe++;
copy_data = (u8 *) data + i * 96;
--
2.19.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] RDMA/cxgb3: Fix unintended sign extension
2018-10-25 14:31 [PATCH] RDMA/cxgb3: Fix unintended sign extension Colin King
@ 2018-10-25 15:26 ` Steve Wise
2018-10-25 16:03 ` David Laight
1 sibling, 0 replies; 3+ messages in thread
From: Steve Wise @ 2018-10-25 15:26 UTC (permalink / raw)
To: Colin King, Doug Ledford, Jason Gunthorpe, linux-rdma
Cc: kernel-janitors, linux-kernel
On 10/25/2018 9:31 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> In the expression "utx_len << 28", utx_len starts as u8, but is promoted
> to a signed int, then sign-extended to u64. If utx_len is 0xf8 or greater
> then the sign extension will set all the upper bits of utx_cmd which is
> probably not what was intended. Cast to utx_len to u64 to avoid the sign
> extension.
>
> Detected by CoverityScan, CID#138764 ("Unintended sign extension")
>
> Fixes: b038ced7b370 ("RDMA/cxgb3: Add driver for Chelsio T3 RNIC")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> drivers/infiniband/hw/cxgb3/cxio_hal.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/infiniband/hw/cxgb3/cxio_hal.c b/drivers/infiniband/hw/cxgb3/cxio_hal.c
> index dcb4bba522ba..c5cb80ccc6d6 100644
> --- a/drivers/infiniband/hw/cxgb3/cxio_hal.c
> +++ b/drivers/infiniband/hw/cxgb3/cxio_hal.c
> @@ -647,7 +647,7 @@ static int cxio_hal_ctrl_qp_write_mem(struct cxio_rdev *rdev_p, u32 addr,
> wqe += (sizeof(struct t3_bypass_wr) >> 3);
> utx_cmd = (T3_UTX_MEM_WRITE << 28) | (addr + i * 3);
> utx_cmd <<= 32;
> - utx_cmd |= (utx_len << 28) | ((utx_len << 2) + 1);
> + utx_cmd |= ((u64)utx_len << 28) | ((utx_len << 2) + 1);
> *wqe = cpu_to_be64(utx_cmd);
> wqe++;
> copy_data = (u8 *) data + i * 96;
>
Acked-by: Steve Wise <swise@chelsio.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [PATCH] RDMA/cxgb3: Fix unintended sign extension
2018-10-25 14:31 [PATCH] RDMA/cxgb3: Fix unintended sign extension Colin King
2018-10-25 15:26 ` Steve Wise
@ 2018-10-25 16:03 ` David Laight
1 sibling, 0 replies; 3+ messages in thread
From: David Laight @ 2018-10-25 16:03 UTC (permalink / raw)
To: 'Colin King',
Steve Wise, Doug Ledford, Jason Gunthorpe, linux-rdma
Cc: kernel-janitors, linux-kernel
From: Colin King
> Sent: 25 October 2018 15:32
>
> From: Colin Ian King <colin.king@canonical.com>
>
> In the expression "utx_len << 28", utx_len starts as u8, but is promoted
> to a signed int, then sign-extended to u64. If utx_len is 0xf8 or greater
> then the sign extension will set all the upper bits of utx_cmd which is
> probably not what was intended. Cast to utx_len to u64 to avoid the sign
> extension.
RTFC...
utx_len is only ever 1, 2 or 3.
The 'problem' would arise if utx_len << 28 set the high bit.
This can only happen if utx_len is more than 7 (NFI where 0xf8 came from).
In any case the best fix is to use 'unsigned int' for wr_len and utx_len.
There is no point making local variable (or functions parameters/results)
smaller that 'int' unless you explicitly want the arithmetic to wrap.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2018-10-25 16:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-25 14:31 [PATCH] RDMA/cxgb3: Fix unintended sign extension Colin King
2018-10-25 15:26 ` Steve Wise
2018-10-25 16:03 ` David Laight
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).