On 11/12/26 3:58 AM, Yi Liu wrote:
On 11/12/26 13:04, Eric Farman wrote:

+cc iommufd maintainers

On 11/11/26 11:52 PM, Eric Farman wrote:


On 11/10/26 3:51 PM, Omar Elghoul wrote:
On 10/12/26 6:28 AM, Andrey Drobyshev wrote:
In its current form, the mechanism for saving CPR FDs into hash table
has flaws.

The CPR fd hash table uses the same CprFd for both key and value, and
is created with a key destroy function.  Inserting a second element
with the same (name, id) key leads to a use after free.  According to
GLib docs [1], g_hash_table_insert() keeps the old key, replaces the
value with the new element, and destroys the new key.  Thus when
inserting 2 CprFd elements fd1 and fd2 with equal keys we get:

   g_hash_table_insert(table, fd1, fd1);  /* table has fd1 -> fd1 */
   g_hash_table_insert(table, fd2, fd2);  /* table has fd1 -> fd2, fd2 freed */

Any subsequent cpr_find_fd() for that key then returns freed memory.

This is reachable whenever two CPR-aware objects register fds under the same name, and it happens long before an actual CPR, since cpr_save_fd()
is typically called from .realize(), i.e. at boot time.

Apart from the UAF itself, when the user decides to perform CPR migration, on CPR target we'd get corrupted FD from the hash table and fail somewhere
in .realize() for affected devices.

As a solution, let's altogether prohibit adding CprFds with duplicate
keys.  Since FDs are being registered for a subsequent CPR right at the
startup in .realize(), that'll allow us to error out and exit QEMU (or
fail a device hotplug op) gracefully instead of delaying potential
problems for later.  To do so, let's make cpr_save_fd() return 'bool'
instead of 'void' and provide an 'errp' argument to it.  That way caller
becomes responsible for checking the return value and propagating the
error.

Also update all the callers to use the adjusted interface to keep things
compilable and bisectable.

[1] https://docs.gtk.org/glib/type_func.HashTable.insert.html

Fixes: a181df93bb3 ("migration/cpr: use hashtable for cpr fds")
Reviewed-by: Peter Xu <[email protected]>
Reviewed-by: Cédric Le Goater <[email protected]>
Signed-off-by: Andrey Drobyshev <[email protected]>

Hi everyone, FYI we've recently run into a regression on s390x where if
we have a KVM guest with a PCI passthrough device using iommufd and we
hot-unplug the device, the guest becomes fully unresponsive. In the host
dmesg, we can see this message right when the hang begins:

[ 1618.849602] vfio-pci 0001:00:00.0: No device request channel
registered, blocked until released by user

It's worth pointing out that the new error message in this commit does indeed show up in the logs, and points to the problematic device fd:

virtqemud[1172]: internal error: unable to execute QEMU command 'object- add': cpr fd 'iommufd0' id 0 is already registered virtqemud[1172]: internal error: unable to execute QEMU command 'closefd': File descriptor named 'iommufd' not found kernel: mlx5_vfio_pci 0004:00:00.0: No device request channel registered, blocked until released by user



Bisecting qemu leads us to this patch:

de80025348925ba9f56070ce4ec0a5a0c512d450 is the first 'bad' commit
commit de80025348925ba9f56070ce4ec0a5a0c512d450
Author: Andrey Drobyshev <[email protected]>
Date:   Mon Oct 12 13:28:09 2026 +0300

     migration/cpr: forbid saving duplicate CprFds into hash table

We tried to revert the commit to confirm the root cause, but then the
master branch no longer compiles, so unfortunately we were not able to
confirm that, at least without making many changes.

We're wondering if you might have any ideas what's going on there, or
what we could do to try and resolve this issue. Any thoughts would be
much appreciated and I'd be happy to share more info if needed.


Since the routine that unregisters such an fd is cpr_delete_fd(), but that gets skipped as soon as we go through the _set_fd() path, I wonder if the solution starts as something like the following?

diff --git a/backends/iommufd.c b/backends/iommufd.c
index ecf4caee1a..d708948357 100644
--- a/backends/iommufd.c
+++ b/backends/iommufd.c
@@ -161,8 +161,8 @@ void iommufd_backend_disconnect(IOMMUFDBackend *be)
      be->users--;
      if (!be->users) {
          vfio_iommufd_cpr_unregister_iommufd(be);
+        cpr_delete_fd(iommufd_fd_name(be), 0);
          if (be->owned) {
-            cpr_delete_fd(iommufd_fd_name(be), 0);
              close(be->fd);
              be->fd = -1;
          }

What that means for the prepare_delete/finalize paths and/or the failure path in _connect is something else to ponder... Neither of those call cpr_delete_fd() themselves despite closing the fd if it owns the thing.

Hi Eric,

Agreed the iommufd0 duplicate-key error is real and your analysis is
self-consistent. One thought on the fix placement though:

iommufd_backend_disconnect()'s existing if (be->owned) guard around
cpr_delete_fd() mirrors the save side in connect() (be->owned && !be- >users), both tied to be->users - that pairing is fine as-is. The actual
asymmetry is that for !be->owned backends, the save happens
unconditionally in complete(). So the matching delete seems like it
should go in finalize()/prepare_delete() instead.

Hi Yi Liu,

Thanks for the info. I had talked myself out of the finalize/prepare_delete path because of the owned/!owned thing, but that was mostly my unfamiliarity with this code to start with. I agree with your reasoning, and moving this outside of the guard isn't the right thing here.


The below hunk of commit de8002534892 indeed can result in req_trigger
being null. But I haven't yet figured out how the error path would be
triggered in that case.

Yeah, I don't know either, was just looking at how this fd gets set/freed once I got this scenario to start working again. I'll mess around with this today, and see if I can get a patch on the list for -rc1.

Thanks!
Eric


--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -76,7 +76,10 @@ static bool vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
      }

      fd = event_notifier_get_fd(e);
-    vfio_cpr_save_vector_fd(vdev, name, nr, fd);
+    if (!vfio_cpr_save_vector_fd(vdev, name, nr, fd, errp)) {
+        event_notifier_cleanup(e);
+        return false;
+    }
      return true;
  }


Regards,
Yi Liu


Reply via email to