Skip to contentSkip to conversation

tests/qtest/migration: fix fd leak in ufd_version_check

PATCH In review Trieu Huynh 2 participants 2 messages Apr 19 – Apr 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/qtest/migration: fix fd leak in ufd_version_check

From: Trieu Huynh <[email protected]>

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.

---
 tests/qtest/migration/migration-util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tests/qtest/migration/migration-util.c +7 -3
index 2648ad7f61..db66d8b24f 100644
@@ -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)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Trieu Huynh

From: Trieu Huynh <[email protected]>

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).

Quoted 5 lines

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.

Thanks,

Signature · 1 line
Peter Xu