Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

vhost: use struct_size and size_add to compute flex array sizes

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
Message-Id: <20230227214127.3678392-1-jacob.e.keller@intel.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Jacob Keller and committed by
Michael S. Tsirkin
e4be66e5 c384c240

+2 -4
+2 -4
drivers/vhost/vhost.c
··· 436 436 size_t event __maybe_unused = 437 437 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; 438 438 439 - return sizeof(*vq->avail) + 440 - sizeof(*vq->avail->ring) * num + event; 439 + return size_add(struct_size(vq->avail, ring, num), event); 441 440 } 442 441 443 442 static size_t vhost_get_used_size(struct vhost_virtqueue *vq, ··· 445 446 size_t event __maybe_unused = 446 447 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; 447 448 448 - return sizeof(*vq->used) + 449 - sizeof(*vq->used->ring) * num + event; 449 + return size_add(struct_size(vq->used, ring, num), event); 450 450 } 451 451 452 452 static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,