misc: vmw_vmci: Leak queue pair endpoint if detach fails on exit
Subsystems (2)
PATCH 1/1 misc: vmw_vmci: Leak queue pair endpoint if detach fails on exit
When the VMCI guest device is removed, vmci_qp_guest_endpoints_exit() asks the hypervisor to detach each remaining queue pair and then frees its queue pages with qp_guest_endpoint_destroy(), even if the detach hypercall failed.
If the detach fails, the hypervisor may still have the queue pair, and the peer may keep writing to the queue pages after they have gone back to the page allocator. The device reset that follows in vmci_guest_remove_device() comes only after the pages have been freed.
qp_detatch_guest_work() already handles the same failure by keeping the entry, because the peer "might still be accessing the shared memory", and leaves the cleanup to this exit path, which then frees it anyway.
Leak the endpoint instead if the detach fails. This also keeps its handle in the resource table, so the handle is not given to a new queue pair while the hypervisor may still have the old one.
---
Compile-tested only (x86_64 with VMWARE_VMCI, W=1); I do not have a VMware setup to test it on.
drivers/misc/vmw_vmci/vmci_queue_pair.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/misc/vmw_vmci/vmci_queue_pair.c +11 -4
@@ -2391,28 +2391,35 @@ int vmci_qp_broker_unmap(struct vmci_handle handle,/* * Destroys all guest queue pair endpoints. If active guest queue * pairs still exist, hypercalls to attempt detach from these -* queue pairs will be made. Any failure to detach is silently -* ignored. +* queue pairs will be made. If a detach fails, the endpoint is +* leaked, as the peer may still access its queues. */ void vmci_qp_guest_endpoints_exit (void ) {struct qp_entry *entry;struct qp_guest_endpoint *ep; +int result;mutex_lock (&qp_guest_endpoints.mutex);while ((entry =qp_list_get_head (&qp_guest_endpoints))) { ep = (struct qp_guest_endpoint *)entry; + result =VMCI_SUCCESS ;/* Don't make a hypercall for local queue_pairs. */ if (!(entry->flags &VMCI_QPFLAG_LOCAL )) -qp_detatch_hypercall (entry->handle); + result =qp_detatch_hypercall (entry->handle);/* We cannot fail the exit, so let's reset ref_count. */ entry->ref_count =0 ;qp_list_remove_entry (&qp_guest_endpoints, entry); -qp_guest_endpoint_destroy (ep); +/* +* If the detach failed, the peer may still access the +* queues, so leak the endpoint. +*/ +if (result >=VMCI_SUCCESS ) +qp_guest_endpoint_destroy (ep); }mutex_unlock (&qp_guest_endpoints.mutex);
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0
Vishnu Dasa
maintainerQuoted 15 lines · Yuho Choi
When the VMCI guest device is removed,
vmci_qp_guest_endpoints_exit()asks the hypervisor to detach each remaining queue pair and then frees its queue pages withqp_guest_endpoint_destroy(), even if the detach hypercall failed.If the detach fails, the hypervisor may still have the queue pair, and the peer may keep writing to the queue pages after they have gone back to the page allocator. The device reset that follows in
vmci_guest_remove_device()comes only after the pages have been freed.
qp_detatch_guest_work()already handles the same failure by keeping the entry, because the peer "might still be accessing the shared memory", and leaves the cleanup to this exit path, which then frees it anyway.
Quoted 6 lines
Leak the endpoint instead if the detach fails. This also keeps its handle in the resource table, so the handle is not given to a new queue pair while the hypervisor may still have the old one.
Fixes 06164d2b72aa ("VMCI: queue pairs implementation.")Signed-off-by Yuho Choi <[email protected]>
Thanks for the fix. It makes sense.
Quoted 12 lines
---
Compile-tested only (x86_64withVMWARE_VMCI, W=1); I do not have a VMware setup to test it on.drivers/misc/vmw_vmci/vmci_queue_pair.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/misc/vmw_vmci/vmci_queue_pair.c +0
-0
drivers/misc/vmw_vmci/vmci_queue_pair.c int vmci_qp_broker_unmap(struct vmci_handle handle, · line 2395
Quoted 7 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +2
-2@@ -2391,28 +2391,35 @@ int vmci_qp_broker_unmap(struct vmci_handle handle,/* * Destroys all guest queue pair endpoints. If active guest queue * pairs still exist, hypercalls to attempt detach from these -* queue pairs will be made. Any failure to detach is silently -* ignored. +* queue pairs will be made. If a detach fails, the endpoint is +* leaked, as the peer may still access its queues.
IMHO, this comment could be a little clearer. For example:
"Destroys all guest queue pair endpoints. If active guest queue pairs still exist, hypercalls to attempt detach from these queue pairs will be made.
If a detach hypercall fails, the hypervisor may still have the queue pair's pages mapped. Freeing them can cause a use-after-free bug if the peer continues to access the pages. The endpoint is therefore intentionally leaked instead."
Quoted 19 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +3
-1*/ void vmci_qp_guest_endpoints_exit (void ) {struct qp_entry *entry;struct qp_guest_endpoint *ep; +int result;mutex_lock (&qp_guest_endpoints.mutex);while ((entry =qp_list_get_head (&qp_guest_endpoints))) { ep = (struct qp_guest_endpoint *)entry; + result =VMCI_SUCCESS ;/* Don't make a hypercall for local queue_pairs. */ if (!(entry->flags &VMCI_QPFLAG_LOCAL )) -qp_detatch_hypercall (entry->handle); + result =qp_detatch_hypercall (entry->handle);/* We cannot fail the exit, so let's reset ref_count. */
drivers/misc/vmw_vmci/vmci_queue_pair.c int vmci_qp_broker_unmap(struct vmci_handle handle, · line 2422
Quoted 10 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +6
-1entry->ref_count =0 ;qp_list_remove_entry (&qp_guest_endpoints, entry); -qp_guest_endpoint_destroy (ep); +/* +* If the detach failed, the peer may still access the +* queues, so leak the endpoint. +*/ +if (result >=VMCI_SUCCESS ) +qp_guest_endpoint_destroy (ep);
Could you add an else branch that does a pr_warn() with the handle and the detach result, so a leaked queue pair can be matched to a cause? Otherwise, this intentional leak will be silent.
Quoted 8 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +0
-0}mutex_unlock (&qp_guest_endpoints.mutex);base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4--
2.43.0