virtio_ring: reject a split ring size that is not a power of two again
v2 cover virtio_ring: reject a split ring size that is not a power of two again
The split ring indexes its rings with num - 1 as a mask, so its size must be a power of two. vring_new_virtqueue() lost that check in v4.6, with commit 2a2d1382fe9d ("virtio: Add improved queue allocation API"), and remoteproc passes it a ring size taken from firmware. A firmware that asks for, e.g. 100 entries gets a ring on which the driver and the device disagree on where the buffers are, so the device can read the wrong ones. The only thing remoteproc checks is that ring size and alignment are not zero. The other caller, mlxbf-tmfifo, uses a hardcoded size of 1024, so it is not affected unless that value is changed.
Patch 1 puts the check back; patch 2 adds a vringh_test case for it.
Tested in userspace with tools/virtio only, not on remoteproc hardware.
Changes in v2:
- Patch 1: log with dev_err() rather than dev_warn(), and say in the message why the length is bad (Francesco)
- Patch 2: commit message updated for the new message text
v1: https://lore.kernel.org/r/[email protected]
Stjepan Poljak (2):
virtio_ring: reject a split ring size that is not a power of two again tools/virtio: vringh_test: check that a split ring must be a power of
two
drivers/virtio/virtio_ring.c | 7 +++++++ tools/virtio/vringh_test.c | 11 +++++++++++ 2 files changed, 18 insertions(+)
base-commit: 1a577f9402c1780cc1e663f48d5dd54c02051b09
Signature · 1 line
2.53.0
PATCH v2 1/2 virtio_ring: reject a split ring size that is not a power of two again
The split ring indexes its avail and used rings with num - 1 as a mask, so its size must be a power of two. Commit 2a2d1382fe9d ("virtio: Add improved queue allocation API") moved that check from vring_new_virtqueue() into the new allocation path, which vring_new_virtqueue() does not use.
remoteproc calls vring_new_virtqueue() with a ring size from the firmware, checked only to be non-zero. With 100 entries, for example, the driver would put buffers 4 and 8 in avail slot 0. The device would look for them at slots 4 and 8, possibly reading the wrong data.
Put the check back for the split ring; the packed ring has no such restriction.
---
remoteproc still reports this as -ENOMEM, although the "Bad virtqueue length" error names the cause. Returning an error pointer from vring_new_virtqueue() is left for later; the same change for vring_create_virtqueue() is here:
https://lore.kernel.org/r/[email protected]
v2:
- Log with dev_err() rather than dev_warn(), and say in the message why the length is bad (Francesco)
--- drivers/virtio/virtio_ring.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/virtio/virtio_ring.c +7 -0
@@ -3513,6 +3513,13 @@ struct virtqueue *vring_new_virtqueue(unsigned int index, name, map); } +/* The split ring indexes its rings with num - 1 as a mask. */ +if (!is_power_of_2 (num)) { +dev_err (&vdev->dev, +"Bad virtqueue length %u (not a power of two)\n" , num); +return NULL ; + } +vring_init (&vring_split.vring, num, pages, vring_align); return __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barriers, context, notify, callback, name,
Signature · 1 line
2.53.0
Quoted 51 lines · Stjepan Poljak
The split ring indexes its avail and used rings with num - 1 as a mask, so its size must be a power of two. Commit
2a2d1382fe9d("virtio: Add improved queue allocation API") moved that check fromvring_new_virtqueue()into the new allocation path, whichvring_new_virtqueue()does not use.remoteproc calls
vring_new_virtqueue()with a ring size from the firmware, checked only to be non-zero. With 100 entries, for example, the driver would put buffers 4 and 8 in avail slot 0. The device would look for them at slots 4 and 8, possibly reading the wrong data.Put the check back for the split ring; the packed ring has no such restriction.
Fixes 2a2d1382fe9d ("virtio: Add improved queue allocation API")Signed-off-by Stjepan Poljak <[email protected]>---
remoteproc still reports this as-ENOMEM, although the "Bad virtqueue length" error names the cause. Returning an error pointer fromvring_new_virtqueue()is left for later; the same change forvring_create_virtqueue()is here:
https://lore.kernel.org/r/[email protected]v2:
- Log withdev_err()rather thandev_warn(), and say in the message why the length is bad (Francesco)--- drivers/virtio/virtio_ring.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/virtio/virtio_ring.c +7
-0@@ -3513,6 +3513,13 @@ struct virtqueue *vring_new_virtqueue(unsigned int index, name, map); } +/* The split ring indexes its rings with num - 1 as a mask. */ +if (!is_power_of_2 (num)) { +dev_err (&vdev->dev, +"Bad virtqueue length %u (not a power of two)\n" , num); +return NULL ; + } +vring_init (&vring_split.vring, num, pages, vring_align); return __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barriers, context, notify, callback, name,Signature · 1 line
2.53.0
Quoted 12 lines · Stjepan Poljak
The split ring indexes its avail and used rings with num - 1 as a mask, so its size must be a power of two. Commit
2a2d1382fe9d("virtio: Add improved queue allocation API") moved that check fromvring_new_virtqueue()into the new allocation path, whichvring_new_virtqueue()does not use.remoteproc calls
vring_new_virtqueue()with a ring size from the firmware, checked only to be non-zero. With 100 entries, for example, the driver would put buffers 4 and 8 in avail slot 0. The device would look for them at slots 4 and 8, possibly reading the wrong data.
Quoted 4 lines
Put the check back for the split ring; the packed ring has no such restriction.
Fixes 2a2d1382fe9d ("virtio: Add improved queue allocation API")
Thanks!
Quoted 36 lines
Signed-off-by Stjepan Poljak <[email protected]>---
remoteproc still reports this as-ENOMEM, although the "Bad virtqueue length" error names the cause. Returning an error pointer fromvring_new_virtqueue()is left for later; the same change forvring_create_virtqueue()is here:
https://lore.kernel.org/r/[email protected]v2:
- Log withdev_err()rather thandev_warn(), and say in the message why the length is bad (Francesco)--- drivers/virtio/virtio_ring.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/virtio/virtio_ring.c +7
-1@@ -3513,6 +3513,13 @@ struct virtqueue *vring_new_virtqueue(unsigned int index, name, map); } +/* The split ring indexes its rings with num - 1 as a mask. */ +if (!is_power_of_2 (num)) { +dev_err (&vdev->dev, +"Bad virtqueue length %u (not a power of two)\n" , num); +return NULL ; + } +vring_init (&vring_split.vring, num, pages, vring_align); return __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barriers, context, notify, callback, name, --2.53.0
PATCH v2 2/2 tools/virtio: vringh_test: check that a split ring must be a power of two
In reply to Stjepan Poljakvring_new_virtqueue() used to accept a split ring of any size, although the ring is indexed with num - 1 as a mask. Check that it now refuses one size short of RINGSIZE.
Without the preceding fix, vringh_test fails with:
vringh_test: vring_new_virtqueue accepted size 255
With it, vring_new_virtqueue() prints:
Bad virtqueue length 255 (not a power of two)
and the rest of the test runs as before.
--- tools/virtio/vringh_test.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/virtio/vringh_test.c +11 -0
@@ -494,6 +494,17 @@ int main(int argc, char *argv[]) __user_addr_max = __user_addr_min +USER_MEM ;memset (__user_addr_min,0 ,vring_size (RINGSIZE ,ALIGN )); +/* +* The split ring indexes its rings with num - 1 as a mask, so a +* size that is not a power of two must be refused. +*/ + vq =vring_new_virtqueue (0 ,RINGSIZE -1 ,ALIGN , &vdev, true, false, + __user_addr_min, + never_notify_host, never_callback_guest, +"guest vq" ); +if (vq) +errx (1 ,"vring_new_virtqueue accepted size %u" ,RINGSIZE -1 ); +/* Set up guest side. */ vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true, false, __user_addr_min,
Signature · 1 line
2.53.0