qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] contrib/vhost-user-blk: fix overflowing expression
@ 2024-07-12 15:38 Stefano Garzarella
  2024-07-12 15:44 ` Peter Maydell
  0 siblings, 1 reply; 2+ messages in thread
From: Stefano Garzarella @ 2024-07-12 15:38 UTC (permalink / raw)
  To: qemu-devel
  Cc: Michael S. Tsirkin, Raphael Norwitz, Stefano Garzarella,
	changpeng.liu, Peter Maydell

Coverity reported:

  >>>     CID 1549454:  Integer handling issues  (OVERFLOW_BEFORE_WIDEN)
  >>>     Potentially overflowing expression
          "le32_to_cpu(desc->num_sectors) << 9" with type "uint32_t"
          (32 bits, unsigned) is evaluated using 32-bit arithmetic, and
          then used in a context that expects an expression of type
          "uint64_t" (64 bits, unsigned).
  199                               le32_to_cpu(desc->num_sectors) << 9 };

Coverity noticed this issue after commit ab04420c3 ("contrib/vhost-user-*:
use QEMU bswap helper functions"), but it was pre-existing and introduced
from the beginning by commit caa1ee4313 ("vhost-user-blk: add
discard/write zeroes features support").

Explicitly cast the 32-bit value before the shift to fix this issue.

Fixes: Coverity CID 1549454
Fixes: 5ab04420c3 ("contrib/vhost-user-*: use QEMU bswap helper functions")
Fixes: caa1ee4313 ("vhost-user-blk: add discard/write zeroes features support")
Cc: changpeng.liu@intel.com
Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 contrib/vhost-user-blk/vhost-user-blk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/vhost-user-blk/vhost-user-blk.c b/contrib/vhost-user-blk/vhost-user-blk.c
index 9492146855..6cc18a1c04 100644
--- a/contrib/vhost-user-blk/vhost-user-blk.c
+++ b/contrib/vhost-user-blk/vhost-user-blk.c
@@ -196,7 +196,7 @@ vub_discard_write_zeroes(VubReq *req, struct iovec *iov, uint32_t iovcnt,
     VubDev *vdev_blk = req->vdev_blk;
     desc = buf;
     uint64_t range[2] = { le64_to_cpu(desc->sector) << 9,
-                          le32_to_cpu(desc->num_sectors) << 9 };
+                          (uint64_t)le32_to_cpu(desc->num_sectors) << 9 };
     if (type == VIRTIO_BLK_T_DISCARD) {
         if (ioctl(vdev_blk->blk_fd, BLKDISCARD, range) == 0) {
             g_free(buf);
-- 
2.45.2



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

* Re: [PATCH] contrib/vhost-user-blk: fix overflowing expression
  2024-07-12 15:38 [PATCH] contrib/vhost-user-blk: fix overflowing expression Stefano Garzarella
@ 2024-07-12 15:44 ` Peter Maydell
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Maydell @ 2024-07-12 15:44 UTC (permalink / raw)
  To: Stefano Garzarella
  Cc: qemu-devel, Michael S. Tsirkin, Raphael Norwitz, changpeng.liu

On Fri, 12 Jul 2024 at 16:39, Stefano Garzarella <sgarzare@redhat.com> wrote:
>
> Coverity reported:
>
>   >>>     CID 1549454:  Integer handling issues  (OVERFLOW_BEFORE_WIDEN)
>   >>>     Potentially overflowing expression
>           "le32_to_cpu(desc->num_sectors) << 9" with type "uint32_t"
>           (32 bits, unsigned) is evaluated using 32-bit arithmetic, and
>           then used in a context that expects an expression of type
>           "uint64_t" (64 bits, unsigned).
>   199                               le32_to_cpu(desc->num_sectors) << 9 };
>
> Coverity noticed this issue after commit ab04420c3 ("contrib/vhost-user-*:
> use QEMU bswap helper functions"), but it was pre-existing and introduced
> from the beginning by commit caa1ee4313 ("vhost-user-blk: add
> discard/write zeroes features support").
>
> Explicitly cast the 32-bit value before the shift to fix this issue.
>
> Fixes: Coverity CID 1549454
> Fixes: 5ab04420c3 ("contrib/vhost-user-*: use QEMU bswap helper functions")
> Fixes: caa1ee4313 ("vhost-user-blk: add discard/write zeroes features support")
> Cc: changpeng.liu@intel.com
> Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---
>  contrib/vhost-user-blk/vhost-user-blk.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/contrib/vhost-user-blk/vhost-user-blk.c b/contrib/vhost-user-blk/vhost-user-blk.c
> index 9492146855..6cc18a1c04 100644
> --- a/contrib/vhost-user-blk/vhost-user-blk.c
> +++ b/contrib/vhost-user-blk/vhost-user-blk.c
> @@ -196,7 +196,7 @@ vub_discard_write_zeroes(VubReq *req, struct iovec *iov, uint32_t iovcnt,
>      VubDev *vdev_blk = req->vdev_blk;
>      desc = buf;
>      uint64_t range[2] = { le64_to_cpu(desc->sector) << 9,
> -                          le32_to_cpu(desc->num_sectors) << 9 };
> +                          (uint64_t)le32_to_cpu(desc->num_sectors) << 9 };
>      if (type == VIRTIO_BLK_T_DISCARD) {
>          if (ioctl(vdev_blk->blk_fd, BLKDISCARD, range) == 0) {
>              g_free(buf);
> --

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM


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

end of thread, other threads:[~2024-07-12 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-12 15:38 [PATCH] contrib/vhost-user-blk: fix overflowing expression Stefano Garzarella
2024-07-12 15:44 ` Peter Maydell

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).