Skip to contentSkip to conversation

vhost-vsock: fix use after free

PATCH Superseded Jason Wang 3 participants 9 messages Sep 27, 2018 – Oct 8, 2018

Subsystems (6)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH v2 1/1 vhost-vsock: fix use after free

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi <[email protected]>

---
- V2: fix typos
- The patch is needed for -stable.

---
 drivers/vhost/vsock.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/vhost/vsock.c +19 -7
index 34bc3ab40c6d..7d0b292867fd 100644
@@ -210,21 +210,27 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
 	struct vhost_vsock *vsock;
 	int len = pkt->len;
 
+	spin_lock_bh(&vhost_vsock_lock);
+
 	/* Find the vhost_vsock according to guest context id  */
-	vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
+	vsock = __vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
 	if (!vsock) {
 		virtio_transport_free_pkt(pkt);
+		spin_unlock_bh(&vhost_vsock_lock);
 		return -ENODEV;
 	}
 
 	if (pkt->reply)
 		atomic_inc(&vsock->queued_replies);
 
-	spin_lock_bh(&vsock->send_pkt_list_lock);
+	spin_lock(&vsock->send_pkt_list_lock);
 	list_add_tail(&pkt->list, &vsock->send_pkt_list);
-	spin_unlock_bh(&vsock->send_pkt_list_lock);
+	spin_unlock(&vsock->send_pkt_list_lock);
 
 	vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
+
+	spin_unlock_bh(&vhost_vsock_lock);
+
 	return len;
 }
 
@@ -236,18 +242,22 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)
 	int cnt = 0;
 	LIST_HEAD(freeme);
 
+	spin_lock_bh(&vhost_vsock_lock);
+
 	/* Find the vhost_vsock according to guest context id  */
-	vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
-	if (!vsock)
+	vsock = __vhost_vsock_get(vsk->remote_addr.svm_cid);
+	if (!vsock) {
+		spin_unlock_bh(&vhost_vsock_lock);
 		return -ENODEV;
+	}
 
-	spin_lock_bh(&vsock->send_pkt_list_lock);
+	spin_lock(&vsock->send_pkt_list_lock);
 	list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
 		if (pkt->vsk != vsk)
 			continue;
 		list_move(&pkt->list, &freeme);
 	}
-	spin_unlock_bh(&vsock->send_pkt_list_lock);
+	spin_unlock(&vsock->send_pkt_list_lock);
 
 	list_for_each_entry_safe(pkt, n, &freeme, list) {
 		if (pkt->reply)
@@ -265,6 +275,8 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)
 			vhost_poll_queue(&tx_vq->poll);
 	}
 
+	spin_unlock_bh(&vhost_vsock_lock);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi <[email protected]>
Quoted 6 lines

---
- V2: fix typos
- The patch is needed for -stable.

---
 drivers/vhost/vsock.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

Thank you, Jason!

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
In reply to Jason Wang
Quoted 11 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi <[email protected]>

Wow is that really the best we can do? A global lock on a data path operation? Granted use after free is nasty but Stefan said he sees a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Quoted 80 lines

---
- V2: fix typos
- The patch is needed for -stable.

---
 drivers/vhost/vsock.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/vhost/vsock.c +19 -7
index 34bc3ab40c6d..7d0b292867fd 100644
@@ -210,21 +210,27 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
 	struct vhost_vsock *vsock;
 	int len = pkt->len;
 
+	spin_lock_bh(&vhost_vsock_lock);
+
 	/* Find the vhost_vsock according to guest context id  */
-	vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
+	vsock = __vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
 	if (!vsock) {
 		virtio_transport_free_pkt(pkt);
+		spin_unlock_bh(&vhost_vsock_lock);
 		return -ENODEV;
 	}
 
 	if (pkt->reply)
 		atomic_inc(&vsock->queued_replies);
 
-	spin_lock_bh(&vsock->send_pkt_list_lock);
+	spin_lock(&vsock->send_pkt_list_lock);
 	list_add_tail(&pkt->list, &vsock->send_pkt_list);
-	spin_unlock_bh(&vsock->send_pkt_list_lock);
+	spin_unlock(&vsock->send_pkt_list_lock);
 
 	vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
+
+	spin_unlock_bh(&vhost_vsock_lock);
+
 	return len;
 }
 
@@ -236,18 +242,22 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)
 	int cnt = 0;
 	LIST_HEAD(freeme);
 
+	spin_lock_bh(&vhost_vsock_lock);
+
 	/* Find the vhost_vsock according to guest context id  */
-	vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
-	if (!vsock)
+	vsock = __vhost_vsock_get(vsk->remote_addr.svm_cid);
+	if (!vsock) {
+		spin_unlock_bh(&vhost_vsock_lock);
 		return -ENODEV;
+	}
 
-	spin_lock_bh(&vsock->send_pkt_list_lock);
+	spin_lock(&vsock->send_pkt_list_lock);
 	list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
 		if (pkt->vsk != vsk)
 			continue;
 		list_move(&pkt->list, &freeme);
 	}
-	spin_unlock_bh(&vsock->send_pkt_list_lock);
+	spin_unlock(&vsock->send_pkt_list_lock);
 
 	list_for_each_entry_safe(pkt, n, &freeme, list) {
 		if (pkt->reply)
@@ -265,6 +275,8 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)
 			vhost_poll_queue(&tx_vq->poll);
 	}
 
+	spin_unlock_bh(&vhost_vsock_lock);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
author
#3 lore raw
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Quoted 8 lines
Quoted 7 lines

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi<[email protected]>

Wow is that really the best we can do?

For net/stable, probably yes.

Quoted 2 lines

A global lock on a data path
operation?

It's already there, and the patch only increase the critical section.

Quoted 4 lines

Granted use after free is nasty but Stefan said he sees
a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Stefan has acked the pacth, so I think it's ok? We can do optimization for -next on top.

Thanks

#4 lore raw
Quoted 15 lines · Jason Wang
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi<[email protected]>
Quoted 8 lines
Quoted 1 line

Wow is that really the best we can do?

For net/stable, probably yes.

Quoted 2 lines

A global lock on a data path
operation?

It's already there,

&vhost_vsock_lock? were is it takes on data path?

Quoted 11 lines

and the patch only increase the critical section.

Quoted 4 lines

Granted use after free is nasty but Stefan said he sees
a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Stefan has acked the pacth, so I think it's ok? We can do optimization for -next on top.

Thanks

Well on high SMP serializing can drop performance as much as x100 so I'm not sure it's appropriate - seems to fix a bug but can introduce a regression. Let's see how does a proper fix look first?

Signature · 1 line
MST
author
#5 lore raw
Quoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · Jason Wang
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi<[email protected]>
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Wow is that really the best we can do?

For net/stable, probably yes.

Quoted 2 lines

A global lock on a data path
operation?

It's already there,

&vhost_vsock_lock? were is it takes on data path?

Ok, but the current code use list which means a global lock is needed anyway here.

Quoted 7 lines
Quoted 7 lines

and the patch only increase the critical section.

Quoted 4 lines

Granted use after free is nasty but Stefan said he sees
a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Quoted 9 lines
Quoted 4 lines

Stefan has acked the pacth, so I think it's ok? We can do optimization for -next on top.

Thanks

Well on high SMP serializing can drop performance as much as x100 so I'm not sure it's appropriate - seems to fix a bug but can introduce a regression. Let's see how does a proper fix look first?

It looks to me hlist + RCU is better. But I'm not sure it's suitable for -net/-stable.

Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#6 lore raw
In reply to Jason Wang
Quoted 15 lines · Jason Wang
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi<[email protected]>
Quoted 8 lines
Quoted 1 line

Wow is that really the best we can do?

For net/stable, probably yes.

Quoted 2 lines

A global lock on a data path
operation?

It's already there,

&vhost_vsock_lock? were is it takes on data path?

Quoted 11 lines

and the patch only increase the critical section.

Quoted 4 lines

Granted use after free is nasty but Stefan said he sees
a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Stefan has acked the pacth, so I think it's ok? We can do optimization for -next on top.

Thanks

Well on high SMP serializing can drop performance as much as x100 so I'm not sure it's appropriate - seems to fix a bug but can introduce a regression. Let's see how does a proper fix look first?

Signature · 5 lines
MST
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author
#7 lore raw
In reply to Michael S. Tsirkin
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Quoted 8 lines
Quoted 7 lines

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi<[email protected]>

Wow is that really the best we can do?

For net/stable, probably yes.

Quoted 2 lines

A global lock on a data path
operation?

It's already there, and the patch only increase the critical section.

Quoted 4 lines

Granted use after free is nasty but Stefan said he sees
a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Stefan has acked the pacth, so I think it's ok? We can do optimization for -next on top.

Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#8 lore raw
In reply to Jason Wang
Quoted 11 lines · Jason Wang

The access of vsock is not protected by vhost_vsock_lock. This may lead to use after free since vhost_vsock_dev_release() may free the pointer at the same time.

Fix this by holding the lock during the access.

Reported-by [email protected]
Fixes 16320f363ae1 ("vhost-vsock: add pkt cancel capability")
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc Stefan Hajnoczi <[email protected]>

Wow is that really the best we can do? A global lock on a data path operation? Granted use after free is nasty but Stefan said he sees a way to fix it using a per socket refcount. He's on vacation until Oct 4 though ...

Quoted 80 lines

---
- V2: fix typos
- The patch is needed for -stable.

---
 drivers/vhost/vsock.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/vhost/vsock.c +19 -7
index 34bc3ab40c6d..7d0b292867fd 100644
@@ -210,21 +210,27 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
 	struct vhost_vsock *vsock;
 	int len = pkt->len;
 
+	spin_lock_bh(&vhost_vsock_lock);
+
 	/* Find the vhost_vsock according to guest context id  */
-	vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
+	vsock = __vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
 	if (!vsock) {
 		virtio_transport_free_pkt(pkt);
+		spin_unlock_bh(&vhost_vsock_lock);
 		return -ENODEV;
 	}
 
 	if (pkt->reply)
 		atomic_inc(&vsock->queued_replies);
 
-	spin_lock_bh(&vsock->send_pkt_list_lock);
+	spin_lock(&vsock->send_pkt_list_lock);
 	list_add_tail(&pkt->list, &vsock->send_pkt_list);
-	spin_unlock_bh(&vsock->send_pkt_list_lock);
+	spin_unlock(&vsock->send_pkt_list_lock);
 
 	vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
+
+	spin_unlock_bh(&vhost_vsock_lock);
+
 	return len;
 }
 
@@ -236,18 +242,22 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)
 	int cnt = 0;
 	LIST_HEAD(freeme);
 
+	spin_lock_bh(&vhost_vsock_lock);
+
 	/* Find the vhost_vsock according to guest context id  */
-	vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
-	if (!vsock)
+	vsock = __vhost_vsock_get(vsk->remote_addr.svm_cid);
+	if (!vsock) {
+		spin_unlock_bh(&vhost_vsock_lock);
 		return -ENODEV;
+	}
 
-	spin_lock_bh(&vsock->send_pkt_list_lock);
+	spin_lock(&vsock->send_pkt_list_lock);
 	list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
 		if (pkt->vsk != vsk)
 			continue;
 		list_move(&pkt->list, &freeme);
 	}
-	spin_unlock_bh(&vsock->send_pkt_list_lock);
+	spin_unlock(&vsock->send_pkt_list_lock);
 
 	list_for_each_entry_safe(pkt, n, &freeme, list) {
 		if (pkt->reply)
@@ -265,6 +275,8 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)
 			vhost_poll_queue(&tx_vq->poll);
 	}
 
+	spin_unlock_bh(&vhost_vsock_lock);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1