[PATCH] selftests/memfd: close fd in sysctl helpers

From: zhangchujun

Date: Fri Nov 13 2026 - 03:21:06 EST


The three sysctl helpers sysctl_assert_write(), sysctl_fail_write()
and sysctl_assert_equal() open /proc/sys/vm/memfd_noexec but never
close the descriptor, leaking one fd on every call. The noexec tests
call these helpers in a loop, so the test can run out of file
descriptors on a system with a low limit.

Close the fd before returning from each helper.

Signed-off-by: zhangchujun <zhangchujun@xxxxxxxxxxxxxxxxxxxx>

diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index cdab3a837624..8bdfa91d3b76 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -120,6 +120,8 @@ static void sysctl_assert_write(const char *val)
printf("write sysctl %s failed: %m\n", val);
abort();
}
+
+ close(fd);
}

static void sysctl_fail_write(const char *val)
@@ -136,6 +138,8 @@ static void sysctl_fail_write(const char *val)
val);
abort();
}
+
+ close(fd);
}

static void sysctl_assert_equal(const char *val)
@@ -153,6 +157,8 @@ static void sysctl_assert_equal(const char *val)
abort();
}

+ close(fd);
+
/* Strip trailing whitespace. */
p = buf;
while (!isspace(*p))
--
2.50.1.windows.1