vhost-scsi: do not validate vrings during endpoint teardown
Subsystems (6)
PATCH 1/1 vhost-scsi: do not validate vrings during endpoint teardown
vhost_scsi_clear_endpoint() checks every virtqueue with vhost_vq_access_ok() before disabling the backends and waiting for in-flight SCSI commands. If that check fails, endpoint teardown is skipped.
vhost_scsi_release() ignores the error and frees the vhost-scsi state. vhost_dev_stop() drains vhost work, but it does not wait for commands owned by the target core. A later command or TMF completion can therefore dereference its saved virtqueue pointer after the virtqueue has been freed.
Vring access is not a prerequisite for tearing an endpoint down. Remove the check so teardown always disables the backends and waits for in-flight commands before releasing their state.
--- drivers/vhost/scsi.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/vhost/scsi.c +1 -8
@@ -2194,17 +2194,10 @@ vhost_scsi_clear_endpoint(struct vhost_scsi *vs,struct vhost_scsi_tpg *tpg;struct vhost_virtqueue *vq;bool match = false; -int index, ret, i; +int ret, i;u8 target;mutex_lock (&vs->dev.mutex); -/* Verify that ring has been setup correctly. */ -for (index =0 ; index < vs->dev.nvqs; ++index) { -if (!vhost_vq_access_ok (&vs->vqs[index].vq)) { - ret = -EFAULT ; - goto err_dev; - } - }if (!vs->vs_tpg) { ret =0 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewThank 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: - [High] vhost-scsi: unhandled…
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:
- [High] vhost-scsi: unhandled vhost_vq_init_access() failure in vhost_scsi_set_endpoint() [1]
--
commit b696a8fa1d0b9b5e5c27a63e36f3d5e5c268720d
Author: Yuho Choi <[email protected]>
vhost-scsi: do not validate vrings during endpoint teardown
This commit removes the vhost_vq_access_ok() check from vhost_scsi_clear_endpoint() so that endpoint teardown always disables the virtqueue backends and waits for in-flight SCSI commands before state is freed.
Quoted 13 lines
drivers/vhost/scsi.c +1
-1@@ -2194,17 +2194,10 @@ vhost_scsi_clear_endpoint(struct vhost_scsi *vs,struct vhost_scsi_tpg *tpg;struct vhost_virtqueue *vq;bool match = false; -int index, ret, i; +int ret, i;u8 target;mutex_lock (&vs->dev.mutex);
drivers/vhost/scsi.c vhost_scsi_clear_endpoint(struct vhost_scsi *vs, · line 2203
Quoted 10 lines
drivers/vhost/scsi.c +0
-7-/* Verify that ring has been setup correctly. */ -for (index =0 ; index < vs->dev.nvqs; ++index) { -if (!vhost_vq_access_ok (&vs->vqs[index].vq)) { - ret = -EFAULT ; - goto err_dev; - } - }if (!vs->vs_tpg) { ret =0 ;
[Severity: High]
This problem wasn't introduced by this patch, but should the paired vhost_scsi_set_endpoint() path check the return value of vhost_vq_init_access() [1]?
drivers/vhost/scsi.c:vhost_scsi_set_endpoint() {
...
for (i = 0; i < vs->dev.nvqs; i++) {
vq = &vs->vqs[i].vq;
if (i >= VHOST_SCSI_VQ_IO && !vs->vqs[i].scsi_cmds)
continue;
mutex_lock(&vq->mutex);
vhost_vq_set_backend(vq, vs_tpg);
vhost_vq_init_access(vq);
mutex_unlock(&vq->mutex);
}
...
}If userspace invokes VHOST_SCSI_SET_ENDPOINT before configuring the vring addresses, vhost_vq_init_access() fails for the control and event virtqueues while their backends remain set to vs_tpg, leaving last_used_idx uninitialized and breaking event delivery and control command handling.
[1] https://sashiko.dev/#/bug/linux-00d9df6c-e264-4a85-ae70-5ede3bace73b
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Mike Christie
maintainerQuoted 8 lines · Yuho Choi
vhost_scsi_clear_endpoint()checks every virtqueue withvhost_vq_access_ok()before disabling the backends and waiting for in-flight SCSI commands. If that check fails, endpoint teardown is skipped.vhost_scsi_release()ignores the error and
vhost_scsi_clear_endpoint()checks every virtqueue withvhost_vq_access_ok()before disabling the backends and waiting for in-flight SCSI commands. If that check fails, endpoint teardown is skipped.
Quoted 9 lines
vhost_scsi_release()ignores the error and frees the vhost-scsi state.vhost_dev_stop()drains vhost work, but it does not wait for commands owned by the target core. A later command or TMF completion can therefore dereference its saved virtqueue pointer after the virtqueue has been freed.Vring access is not a prerequisite for tearing an endpoint down. Remove the check so teardown always disables the backends and waits for in-flight commands before releasing their state.
Can you describe the bug a little more?
If we have done vhost_scsi_set_endpoint successfully, are you saying a later call to vhost_scsi_clear_endpoint could fail the vhost_vq_access_ok check and not teardown the endpoint?
Or are you saying that even if we never did vhost_scsi_set_endpoint then when vhost_scsi_release is called, a command could be running in the vhost-scsi driver and so we need to do something? Is the something a "vhost_vq_set_backend(vq, NULL)" and vhost_scsi_flush(vs)?
Thanks for the review!
Quoted 6 lines · Mike Christie
Can you describe the bug a little more?
If we have done
vhost_scsi_set_endpointsuccessfully, are you saying a later call tovhost_scsi_clear_endpointcould fail the.vhost_vq_access_okcheck and not teardown the endpoint?
Yes, the first case is what I meant. Sorry for not explaining the failure path in the commit message.
Since a543346b96a1 ("vhost-scsi: reject delayed virtqueue setup"), I/O virtqueues that are not configured when SET_ENDPOINT is called remain without a backend.
VHOST_SET_LOG_BASE validates the new log base only for virtqueues with a backend. Therefore, an invalid log base can be stored in an inactive virtqueue even when the ioctl returns -EFAULT. A subsequent CLEAR_ENDPOINT then fails vhost_vq_access_ok() and skips teardown.
I reproduced this on current mainline (8dac6df92a2e) with KASAN, with the following sequence:
VHOST_SET_OWNER
VHOST_SET_FEATURES(VHOST_F_LOG_ALL)
VHOST_SET_MEM_TABLE
Set up vq 0, 1, and 2 only
VHOST_SCSI_SET_ENDPOINT -> 0 VHOST_SET_LOG_BASE(kernel address) -> -EFAULT VHOST_SCSI_CLEAR_ENDPOINT -> -EFAULT
close()
After close(), tpg->vhost_scsi still points to the freed vs. Linking another LUN triggers a KASAN use-after-free in vhost_scsi_do_plug().
I also reproduced the in-flight command UAF described in the commit message. A delayed READ completion after close() triggers KASAN in vhost_scsi_release_cmd(). The READ has to be queued after the VHOST_SET_LOG_BASE call, because device ioctls flush in-flight commands.
With the patch applied, both reproductions complete without KASAN reports, and endpoint teardown succeeds.
Without a successful SET_ENDPOINT, no command can start, so the second scenario you mentioned is not what I meant.
I can share the reproducer and full KASAN reports if useful. I'll include the failure sequence in v2.
Thanks