virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vhost: use struct_size and size_add to compute flex array sizes
@ 2023-02-27 21:41 Jacob Keller
  2023-02-27 21:53 ` Michael S. Tsirkin
  0 siblings, 1 reply; 2+ messages in thread
From: Jacob Keller @ 2023-02-27 21:41 UTC (permalink / raw)
  To: kvm; +Cc: Jacob Keller, virtualization, Michael S. Tsirkin

The vhost_get_avail_size and vhost_get_used_size functions compute the size
of structures with flexible array members with an additional 2 bytes if the
VIRTIO_RING_F_EVENT_IDX feature flag is set. Convert these functions to use
struct_size() and size_add() instead of coding the calculation by hand.

This ensures that the calculations will saturate at SIZE_MAX rather than
overflowing.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: virtualization@lists.linux-foundation.org
Cc: kvm@vger.kernel.org
---

I found this using a coccinelle patch I developed and submitted at [1].

[1]: https://lore.kernel.org/all/20230227202428.3657443-1-jacob.e.keller@intel.com/

 drivers/vhost/vhost.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index f11bdbe4c2c5..43fa626d4e44 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -436,8 +436,7 @@ static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,
 	size_t event __maybe_unused =
 	       vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
 
-	return sizeof(*vq->avail) +
-	       sizeof(*vq->avail->ring) * num + event;
+	return size_add(struct_size(vq->avail, ring, num), event);
 }
 
 static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
@@ -446,8 +445,7 @@ static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
 	size_t event __maybe_unused =
 	       vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
 
-	return sizeof(*vq->used) +
-	       sizeof(*vq->used->ring) * num + event;
+	return size_add(struct_size(vq->used, ring, num), event);
 }
 
 static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
-- 
2.39.1.405.gd4c25cc71f83

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

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

* Re: [PATCH] vhost: use struct_size and size_add to compute flex array sizes
  2023-02-27 21:41 [PATCH] vhost: use struct_size and size_add to compute flex array sizes Jacob Keller
@ 2023-02-27 21:53 ` Michael S. Tsirkin
  0 siblings, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2023-02-27 21:53 UTC (permalink / raw)
  To: Jacob Keller; +Cc: kvm, virtualization

On Mon, Feb 27, 2023 at 01:41:27PM -0800, Jacob Keller wrote:
> The vhost_get_avail_size and vhost_get_used_size functions compute the size
> of structures with flexible array members with an additional 2 bytes if the
> VIRTIO_RING_F_EVENT_IDX feature flag is set. Convert these functions to use
> struct_size() and size_add() instead of coding the calculation by hand.
> 
> This ensures that the calculations will saturate at SIZE_MAX rather than
> overflowing.
> 
> Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Jason Wang <jasowang@redhat.com>
> Cc: virtualization@lists.linux-foundation.org
> Cc: kvm@vger.kernel.org


Acked-by: Michael S. Tsirkin <mst@redhat.com>

Will merge, thanks!
> ---
> 
> I found this using a coccinelle patch I developed and submitted at [1].
> 
> [1]: https://lore.kernel.org/all/20230227202428.3657443-1-jacob.e.keller@intel.com/
> 
>  drivers/vhost/vhost.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index f11bdbe4c2c5..43fa626d4e44 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -436,8 +436,7 @@ static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,
>  	size_t event __maybe_unused =
>  	       vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
>  
> -	return sizeof(*vq->avail) +
> -	       sizeof(*vq->avail->ring) * num + event;
> +	return size_add(struct_size(vq->avail, ring, num), event);
>  }
>  
>  static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
> @@ -446,8 +445,7 @@ static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
>  	size_t event __maybe_unused =
>  	       vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
>  
> -	return sizeof(*vq->used) +
> -	       sizeof(*vq->used->ring) * num + event;
> +	return size_add(struct_size(vq->used, ring, num), event);
>  }
>  
>  static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
> -- 
> 2.39.1.405.gd4c25cc71f83

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

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

end of thread, other threads:[~2023-02-27 21:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-27 21:41 [PATCH] vhost: use struct_size and size_add to compute flex array sizes Jacob Keller
2023-02-27 21:53 ` Michael S. Tsirkin

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