vhost-scsi: fix event queue iov out-of-bounds
Subsystems (6)
PATCH 1/1 vhost-scsi: fix event queue iov out-of-bounds
vhost_scsi_do_evt_work() uses vq->iov[out] after vhost_get_vq_desc() without checking that the descriptor chain contains an input segment. vq->iov has UIO_MAXIOV entries (1024). A chain of 1024 output-only descriptors yields out == 1024 and in == 0, so the length check reads one past the end of the array.
With a host-side harness that sets VHOST_SCSI_SET_EVENTS_MISSED and kicks an event queue filled with 1024 OUT descriptors, UBSAN reports:
UBSAN: array-index-out-of-bounds in drivers/vhost/scsi.c:611:14 index 1024 is out of range for type 'iovec [1024]' Call Trace: <TASK> dump_stack_lvl+0x5f/0x90 dump_stack+0x10/0x18 ubsan_epilogue+0x9/0x39 __ubsan_handle_out_of_bounds.cold+0x50/0x55 vhost_scsi_complete_events+0x55f/0x5a0 [vhost_scsi] vhost_scsi_evt_work+0x17/0x30 [vhost_scsi] vhost_run_work_list+0x8e/0xd0 [vhost] vhost_task_fn+0xe1/0x210 ret_from_fork+0x348/0x540 ret_from_fork_asm+0x1a/0x30 </TASK>
Require at least one input descriptor before indexing iov[out], and treat a pure-output chain as a missed event like other invalid event buffers.
--- drivers/vhost/scsi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/vhost/scsi.c +3 -2
@@ -608,9 +608,10 @@return ; } -if ((vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event ))) { +if (!in || + vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event )) {vq_err (vq,"Expecting virtio_scsi_event, got %zu bytes\n" , - vq->iov[out].iov_len); + in ? vq->iov[out].iov_len :0 ); vs->vs_events_missed = true;return ; }
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Self-deadlock (AA deadlock) in vhost_scsi_send_evt when vhost_vq_work_queue fails. --
commit c17fff972ec26b541af9ec7b514db052328d64e6
Author: Jia Jia <[email protected]>
vhost-scsi: fix event queue iov out-of-bounds
This commit fixes an array-index-out-of-bounds issue in vhost_scsi_do_evt_work() by ensuring there is at least one input descriptor before indexing the iov array. It treats pure-output chains as missed events to prevent reading past the end of the array.
drivers/vhost/scsi.c vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) · line 612
Quoted 11 lines
drivers/vhost/scsi.c +2
-1@@ -608,9 +608,10 @@ vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)return ; } -if ((vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event ))) { +if (!in || + vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event )) {
[Severity: High]
This is a pre-existing issue, but while reviewing this event handling code, I noticed a potential self-deadlock scenario.
If vhost_scsi_send_evt() is called from a path that already holds vq->mutex, like vhost_scsi_evt_handle_kick():
vhost_scsi_evt_handle_kick()
mutex_lock(&vq->mutex);
...
if (vs->vs_events_missed)
vhost_scsi_send_evt(...)And vhost_scsi_send_evt() falls back to vhost_scsi_complete_events() because vhost_vq_work_queue() returns false (for instance, if vq->worker is NULL):
vhost_scsi_send_evt()
if (!vhost_vq_work_queue(vq, &vs->vs_event_work))
vhost_scsi_complete_events(vs, true);vhost_scsi_complete_events() then attempts to take the exact same mutex:
vhost_scsi_complete_events()
mutex_lock(&vq->mutex);
Could this sequence cause a self-deadlock where the kernel thread hangs indefinitely waiting on the non-recursive vq->mutex, potentially leading to a denial of service?
Quoted 6 lines
drivers/vhost/scsi.c +1
-1vq_err (vq,"Expecting virtio_scsi_event, got %zu bytes\n" , - vq->iov[out].iov_len); + in ? vq->iov[out].iov_len :0 ); vs->vs_events_missed = true;return ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 65 lines
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Self-deadlock (AA deadlock) invhost_scsi_send_evtwhenvhost_vq_work_queuefails. --commit
c17fff972ec26b541af9ec7b514db052328d64e6
Author: Jia Jia <[email protected]>vhost-scsi: fix event queue iov out-of-bounds
This commit fixes an array-index-out-of-bounds issue in
vhost_scsi_do_evt_work()by ensuring there is at least one input descriptor before indexing the iov array. It treats pure-output chains as missed events to prevent reading past the end of the array.drivers/vhost/scsi.c vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) · line 612
Quoted 11 lines
drivers/vhost/scsi.c +2
-1@@ -608,9 +608,10 @@ vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)return ; } -if ((vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event ))) { +if (!in || + vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event )) {[Severity: High]
This is a pre-existing issue, but while reviewing this event handling code, I noticed a potential self-deadlock scenario.If
vhost_scsi_send_evt()is called from a path that already holdsvq->mutex, likevhost_scsi_evt_handle_kick():
vhost_scsi_evt_handle_kick()mutex_lock(&vq->mutex); ... if (vs->vs_events_missed) vhost_scsi_send_evt(...)And
vhost_scsi_send_evt()falls back tovhost_scsi_complete_events()becausevhost_vq_work_queue()returns false (for instance, ifvq->workeris NULL):
vhost_scsi_send_evt()if (!vhost_vq_work_queue(vq, &vs->vs_event_work)) vhost_scsi_complete_events(vs, true);
vhost_scsi_complete_events()then attempts to take the exact same mutex:
vhost_scsi_complete_events()mutex_lock(&vq->mutex);Could this sequence cause a self-deadlock where the kernel thread hangs indefinitely waiting on the non-recursive
vq->mutex, potentially leading to a denial of service?drivers/vhost/scsi.c line 617
Quoted 6 lines
drivers/vhost/scsi.c +1
-1vq_err (vq,"Expecting virtio_scsi_event, got %zu bytes\n" , - vq->iov[out].iov_len); + in ? vq->iov[out].iov_len :0 ); vs->vs_events_missed = true;return ; }
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
This is a false positive. The current patch does not introduce this issue, but I will try to reproduce it to verify whether it is a real issue.
Michael S. Tsirkin
maintainerQuoted 26 lines · Jia Jia
vhost_scsi_do_evt_work()uses vq->iov[out] aftervhost_get_vq_desc()without checking that the descriptor chain contains an input segment.vq->iovhasUIO_MAXIOVentries (1024). A chain of 1024 output-only descriptors yields out == 1024 and in == 0, so the length check reads one past the end of the array.With a host-side harness that sets
VHOST_SCSI_SET_EVENTS_MISSEDand kicks an event queue filled with 1024 OUT descriptors, UBSAN reports:UBSAN: array-index-out-of-bounds in drivers/vhost/scsi.c:611:14 index 1024 is out of range for type 'iovec [1024]' Call Trace: <TASK> dump_stack_lvl+0x5f/0x90 dump_stack+0x10/0x18 ubsan_epilogue+0x9/0x39 __ubsan_handle_out_of_bounds.cold+0x50/0x55 vhost_scsi_complete_events+0x55f/0x5a0 [vhost_scsi] vhost_scsi_evt_work+0x17/0x30 [vhost_scsi] vhost_run_work_list+0x8e/0xd0 [vhost] vhost_task_fn+0xe1/0x210 ret_from_fork+0x348/0x540 ret_from_fork_asm+0x1a/0x30 </TASK>
Quoted 6 lines
Require at least one input descriptor before indexing iov[out], and treat a pure-output chain as a missed event like other invalid event buffers.
Fixes a6c9af87363c ("tcm_vhost: Add hotplug/hotunplug support")Signed-off-by Jia Jia <[email protected]>
Applied an alternative more comprehensive patch, credited you.
Quoted 23 lines
--- drivers/vhost/scsi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/vhost/scsi.c +3
-2@@ -608,9 +608,10 @@return ; } -if ((vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event ))) { +if (!in || + vq->iov[out].iov_len !=sizeof (struct virtio_scsi_event )) {vq_err (vq,"Expecting virtio_scsi_event, got %zu bytes\n" , - vq->iov[out].iov_len); + in ? vq->iov[out].iov_len :0 ); vs->vs_events_missed = true;return ; }Signature · 1 line
2.43.0