[Devel] [PATCH hci-8.1 v3 1/2] drivers/vhost: fix a use-after-free of VHOST_SET_NWORKERS workers

Alexander Ivanov alexander.ivanov at virtuozzo.com
Sun Nov 15 16:39:56 MSK 2026


vhost_set_workers() and vhost_propagate_workers() store workers into
vq->worker directly, bypassing __vhost_vq_attach_worker(). The workers
they install keep attachment_cnt at 0, and the default worker keeps
counting every vq. VHOST_FREE_WORKER then destroys a worker a virtqueue
still points at, and the next vhost_vq_work_queue() on that vq is a
use-after-free. VHOST_ATTACH_VRING_WORKER on such a vq drives
attachment_cnt of its worker to -1. vhost-scsi already exposes both
interfaces to the device owner.

Attach the workers with __vhost_vq_attach_worker(), so VHOST_FREE_WORKER
fails with -EBUSY while a vq uses the worker and both interfaces can
coexist until userspace moves to the worker ioctls. Userspace issues
VHOST_SET_NWORKERS before VHOST_SET_VRING_KICK, so the attach takes the
path without synchronize_rcu() and VM startup is not slowed down.

The attach dereferences the worker, so skip a source vq whose worker
was killed, and reject VHOST_SET_NWORKERS on devices that do not use
workers. On vhost-vdpa every vq->worker is NULL, and the workers
vhost_set_workers() created there were never freed.

https://virtuozzo.atlassian.net/browse/VSTOR-107537
Fixes: 1491588e71da ("drivers/vhost: add ioctl to increase the number of workers")
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests

Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
---
 drivers/vhost/vhost.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 1057ba7e3fc7..422cac6f2298 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -1094,7 +1094,12 @@ static void vhost_propagate_workers(struct vhost_dev *dev, int nworkers)
 	int i, j = 0;
 
 	for (i = nworkers; i < dev->nvqs; i++) {
-		dev->vqs[i]->worker = dev->vqs[j]->worker;
+		struct vhost_worker *worker;
+
+		worker = rcu_dereference_check(dev->vqs[j]->worker,
+					       lockdep_is_held(&dev->mutex));
+		if (worker)
+			__vhost_vq_attach_worker(dev->vqs[i], worker);
 		if (++j >= nworkers)
 			j = 0;
 	}
@@ -1117,7 +1122,7 @@ static int vhost_set_workers(struct vhost_dev *dev, int n)
 			ret = -ENOMEM;
 			break;
 		}
-		dev->vqs[i]->worker = worker;
+		__vhost_vq_attach_worker(dev->vqs[i], worker);
 	}
 
 	vhost_propagate_workers(dev, i);
@@ -2445,7 +2450,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = get_user(n, (int __user *)argp);
 		if (r < 0)
 			break;
-		if (d->workers_set) {
+		if (!d->use_worker || d->workers_set) {
 			r = -EINVAL;
 			break;
 		}
-- 
2.53.0



More information about the Devel mailing list