Skip to contentSkip to conversation

vhost: fix a theoretical race in device cleanup

PATCH In review Michael S. Tsirkin 3 participants 4 messages Feb 13, 2014

Subsystems (5)
lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

PATCH v2 1/1 vhost: fix a theoretical race in device cleanup

vhost_zerocopy_callback accesses VQ right after it drops a ubuf reference. In theory, this could race with device removal which waits on the ubuf kref, and crash on use after free.

Do all accesses within rcu read side critical section, and synchronize on release.

Since callbacks are always invoked from bh, synchronize_rcu_bh seems enough and will help release complete a bit faster.

---

This is was previously posted as part of patch series, but it's an independent fix really. Theoretical race so not needed for stable I think.

changes from v1:
fixed typo in commit log

 drivers/vhost/net.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/vhost/net.c +6 -0
index b12176f..f1be80d 100644
@@ -308,6 +308,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
 	struct vhost_virtqueue *vq = ubufs->vq;
 	int cnt;
 
+	rcu_read_lock_bh();
+
 	/* set len to mark this desc buffers done DMA */
 	vq->heads[ubuf->desc].len = success ?
 		VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
@@ -322,6 +324,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
 	 */
 	if (cnt <= 1 || !(cnt % 16))
 		vhost_poll_queue(&vq->poll);
+
+	rcu_read_unlock_bh();
 }
 
 /* Expects to be always run from workqueue - which acts as
@@ -804,6 +808,8 @@ static int vhost_net_release(struct inode *inode, struct file *f)
 		fput(tx_sock->file);
 	if (rx_sock)
 		fput(rx_sock->file);
+	/* Make sure no callbacks are outstanding */
+	synchronize_rcu_bh();
 	/* We do an extra flush before freeing memory,
 	 * since jobs can re-queue themselves. */
 	vhost_net_flush(n);
Signature · 1 line
MST
Acked-by on v2 1/1
#1 lore raw
Quoted 46 lines · Michael S. Tsirkin

vhost_zerocopy_callback accesses VQ right after it drops a ubuf reference. In theory, this could race with device removal which waits on the ubuf kref, and crash on use after free.

Do all accesses within rcu read side critical section, and synchronize on release.

Since callbacks are always invoked from bh, synchronize_rcu_bh seems enough and will help release complete a bit faster.

---

This is was previously posted as part of patch series, but it's an independent fix really. Theoretical race so not needed for stable I think.

changes from v1:
fixed typo in commit log

 drivers/vhost/net.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/vhost/net.c +4 -0
index b12176f..f1be80d 100644
@@ -308,6 +308,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
 	struct vhost_virtqueue *vq = ubufs->vq;
 	int cnt;
 
+	rcu_read_lock_bh();
+
 	/* set len to mark this desc buffers done DMA */
 	vq->heads[ubuf->desc].len = success ?
 		VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
@@ -322,6 +324,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
 	 */
 	if (cnt <= 1 || !(cnt % 16))
 		vhost_poll_queue(&vq->poll);
+
+	rcu_read_unlock_bh();
 }
 
 /* Expects to be always run from workqueue - which acts as

drivers/vhost/net.c static int vhost_net_release(struct inode *inode, struct file *f) · line 815

Quoted 8 lines
drivers/vhost/net.c +2 -0
@@ -804,6 +808,8 @@ static int vhost_net_release(struct inode *inode, struct file *f)
 		fput(tx_sock->file);
 	if (rx_sock)
 		fput(rx_sock->file);
+	/* Make sure no callbacks are outstanding */
+	synchronize_rcu_bh();
 	/* We do an extra flush before freeing memory,
 	 * since jobs can re-queue themselves. */
 	vhost_net_flush(n);
Acked-by on v2 1/1
#2 lore raw
In reply to Michael S. Tsirkin
Quoted 46 lines · Michael S. Tsirkin

vhost_zerocopy_callback accesses VQ right after it drops a ubuf reference. In theory, this could race with device removal which waits on the ubuf kref, and crash on use after free.

Do all accesses within rcu read side critical section, and synchronize on release.

Since callbacks are always invoked from bh, synchronize_rcu_bh seems enough and will help release complete a bit faster.

---

This is was previously posted as part of patch series, but it's an independent fix really. Theoretical race so not needed for stable I think.

changes from v1:
fixed typo in commit log

 drivers/vhost/net.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/vhost/net.c +4 -0
index b12176f..f1be80d 100644
@@ -308,6 +308,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
 	struct vhost_virtqueue *vq = ubufs->vq;
 	int cnt;
 
+	rcu_read_lock_bh();
+
 	/* set len to mark this desc buffers done DMA */
 	vq->heads[ubuf->desc].len = success ?
 		VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
@@ -322,6 +324,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
 	 */
 	if (cnt <= 1 || !(cnt % 16))
 		vhost_poll_queue(&vq->poll);
+
+	rcu_read_unlock_bh();
 }
 
 /* Expects to be always run from workqueue - which acts as

drivers/vhost/net.c static int vhost_net_release(struct inode *inode, struct file *f) · line 815

Quoted 8 lines
drivers/vhost/net.c +2 -0
@@ -804,6 +808,8 @@ static int vhost_net_release(struct inode *inode, struct file *f)
 		fput(tx_sock->file);
 	if (rx_sock)
 		fput(rx_sock->file);
+	/* Make sure no callbacks are outstanding */
+	synchronize_rcu_bh();
 	/* We do an extra flush before freeing memory,
 	 * since jobs can re-queue themselves. */
 	vhost_net_flush(n);
maintainer
#3 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Thu, 13 Feb 2014 11:45:11 +0200

Quoted 10 lines

vhost_zerocopy_callback accesses VQ right after it drops a ubuf reference. In theory, this could race with device removal which waits on the ubuf kref, and crash on use after free.

Do all accesses within rcu read side critical section, and synchronize on release.

Since callbacks are always invoked from bh, synchronize_rcu_bh seems enough and will help release complete a bit faster.

Quoted 6 lines

---

This is was previously posted as part of patch series, but it's an independent fix really. Theoretical race so not needed for stable I think.

Ok, no -stable, applied.