tests/qtest/migration: fix fd leak in ufd_version_check
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
@@ -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
Quoted 7 lines · Trieu Huynh
From: Trieu Huynh <[email protected]>
ufd_version_check()opens a userfaultfd viauffd_open()but never closes it on any path where the open succeeded: theUFFDIO_APIfailure 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 inutil/userfaultfd.candmigration/postcopy-ram.c, ensuring the fd is always closed before returning.Signed-off-by Trieu Huynh <[email protected]>
Thanks,
Signature · 1 line
Peter Xu