[PATCH] tests/qtest/migration: fix fd leak in ufd_version_check

Trieu Huynh posted 1 patch 2 days, 1 hour ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260419110304.8661-1-viking4@gmail.com
Maintainers: Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
tests/qtest/migration/migration-util.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
[PATCH] tests/qtest/migration: fix fd leak in ufd_version_check
Posted by Trieu Huynh 2 days, 1 hour ago
From: Trieu Huynh <vikingtc4@gmail.com>

ufd_version_check() opens a userfaultfd via uffd_open() but never closes
it on any path where the open succeeded: the UFFDIO_API failure path,
the missing-ioctls path, and the success path all returned without
calling close(ufd).

Convert to a goto-out pattern consistent with uffd_open() used in
util/userfaultfd.c and migration/postcopy-ram.c, ensuring the fd is
always closed before returning.

Signed-off-by: Trieu Huynh <vikingtc4@gmail.com>
---
 tests/qtest/migration/migration-util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/tests/qtest/migration/migration-util.c b/tests/qtest/migration/migration-util.c
index 2648ad7f61..db66d8b24f 100644
--- a/tests/qtest/migration/migration-util.c
+++ b/tests/qtest/migration/migration-util.c
@@ -343,6 +343,7 @@ bool ufd_version_check(bool *uffd_feature_thread_id)
 {
     struct uffdio_api api_struct;
     uint64_t ioctl_mask;
+    bool ret = false;
 
     int ufd = uffd_open(O_CLOEXEC);
 
@@ -355,7 +356,7 @@ bool ufd_version_check(bool *uffd_feature_thread_id)
     api_struct.features = 0;
     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
         g_test_message("Skipping test: UFFDIO_API failed");
-        return false;
+        goto release_ufd;
     }
 
     if (uffd_feature_thread_id) {
@@ -366,10 +367,13 @@ bool ufd_version_check(bool *uffd_feature_thread_id)
                   1ULL << _UFFDIO_UNREGISTER);
     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
         g_test_message("Skipping test: Missing userfault feature");
-        return false;
+        goto release_ufd;
     }
 
-    return true;
+    ret = true;
+release_ufd:
+    close(ufd);
+    return ret;
 }
 #else
 bool ufd_version_check(bool *uffd_feature_thread_id)
-- 
2.43.0
Re: [PATCH] tests/qtest/migration: fix fd leak in ufd_version_check
Posted by Peter Xu 17 hours ago
On Sun, Apr 19, 2026 at 06:03:04PM +0700, Trieu Huynh wrote:
> From: Trieu Huynh <vikingtc4@gmail.com>
> 
> ufd_version_check() opens a userfaultfd via uffd_open() but never closes
> it on any path where the open succeeded: the UFFDIO_API failure path,
> the missing-ioctls path, and the success path all returned without
> calling close(ufd).
> 
> Convert to a goto-out pattern consistent with uffd_open() used in
> util/userfaultfd.c and migration/postcopy-ram.c, ensuring the fd is
> always closed before returning.
> 
> Signed-off-by: Trieu Huynh <vikingtc4@gmail.com>

Thanks,

Reviewed-by: Peter Xu <peterx@redhat.com>

-- 
Peter Xu