virtio_ring: reject a split ring size that is not a power of two again
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.
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 | 6 ++++++ tools/virtio/vringh_test.c | 11 +++++++++++ 2 files changed, 17 insertions(+)
base-commit: 1a577f9402c1780cc1e663f48d5dd54c02051b09
Signature · 1 line
2.53.0
PATCH 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" warning 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]
--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/virtio/virtio_ring.c +6 -0
@@ -3513,6 +3513,12 @@ 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_warn (&vdev->dev,"Bad virtqueue length %u\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,
base-commit: 1a577f9402c1780cc1e663f48d5dd54c02051b09
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 11 lines
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 5 lines
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]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Stjepan,
Quoted 11 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.
Good catch - I think I encountered something like this during my experiments with virtio-over-remoteproc and ascribed it to a different cause.
Quoted 20 lines
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" warning 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]--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +0
-0
drivers/virtio/virtio_ring.c struct virtqueue *vring_new_virtqueue(unsigned int index, · line 3518
Quoted 6 lines
drivers/virtio/virtio_ring.c +3
-0@@ -3513,6 +3513,12 @@ 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_warn (&vdev->dev,"Bad virtqueue length %u\n" , num);
This is definitely and error, and should be logged as such (I know, the same message is printed as a warn in vring_alloc_queue_split(), but we have no ABI to align to here).
Also, consider to adopt a message a bit more descriptive; maybe something like this is sufficient:
"Bad virtqueue length %u (not a power of two)\n"
Quoted 7 lines
drivers/virtio/virtio_ring.c +3
-0+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,
Quoted 5 lines
base-commit:
1a577f9402c1780cc1e663f48d5dd54c02051b09Signature · 1 line
2.53.0
Thank you!
Regards,
Francesco
Hi Francesco,
Quoted 23 lines · Francesco Valla
This is definitely and error, and should be logged as such (I know, the same message is printed as a warn in
vring_alloc_queue_split(), but we have no ABI to align to here).Also, consider to adopt a message a bit more descriptive; maybe something like this is sufficient:
"Bad virtqueue length %u (not a power of two)\n"
Quoted 7 lines
drivers/virtio/virtio_ring.c +3
-0+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,Quoted 5 lines
base-commit:
1a577f9402c1780cc1e663f48d5dd54c02051b09--
2.53.0
Quoted 5 lines
Thank you!
Regards,
Francesco
Agreed on both, thanks. v2 uses dev_err() with your message:
https://lore.kernel.org/r/[email protected]
Stjepan
PATCH 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" 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
[email protected]
AI reviewRe: [PATCH 2/2] tools/virtio: vringh_test: check that a split ring must be a power of two
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Medium] tools/virtio: memory leak of…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Medium] tools/virtio: memory leak of virtqueue in main() [1] --
commit dacd77c2e0af2c3d6ebdce34137950d56c8f8e41
Author: Stjepan Poljak <[email protected]>
tools/virtio: vringh_test: check that a split ring must be a power of two
This commit adds a check in vringh_test to verify that vring_new_virtqueue() rejects a non-power-of-two split ring size.
Quoted 12 lines
tools/virtio/vringh_test.c +4
-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. +*/
tools/virtio/vringh_test.c int main(int argc, char *argv[]) · line 510
Quoted 10 lines
tools/virtio/vringh_test.c +7
-0+ 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,
[Severity: Medium]
This problem wasn't introduced by this patch, but does main() leak vq
[1] when running with --indirect?
Later in main(), the VIRTIO_RING_F_INDIRECT_DESC test branch reassigns vq without calling vring_del_virtqueue(vq) first:
tools/virtio/vringh_test.c:main() {
...
if (__virtio_test_bit(&vdev, VIRTIO_RING_F_INDIRECT_DESC)) {
...
__virtio_clear_bit(&vdev, VIRTIO_RING_F_INDIRECT_DESC);
vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true,
false, __user_addr_min,
never_notify_host,
never_callback_guest,
"guest vq");
...
}This leaks the initial virtqueue and leaves it linked on vdev.vqs when the cleanup path at the end of main() only deletes the second vq.
[1] https://sashiko.dev/#/bug/linux-e99ea9bc-10be-4c6a-b7f2-1998001de64b
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2