Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Günther Noack" <gnoack3000@gmail.com>
To: "Mickaël Salaün" <mic@digikod.net>
Cc: "Wang Yan" <wangyan01@kylinos.cn>,
	linux-kselftest@vger.kernel.org,
	"Günther Noack" <gnoack3000@gmail.com>,
	"Günther Noack" <gnoack@google.com>,
	"Shuah Khan" <shuah@kernel.org>,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 15/27] selftests/landlock: Close leaked file descriptors
Date: Thu,  8 Oct 2026 16:25:44 +0200	[thread overview]
Message-ID: <20261008142604.39107-17-gnoack3000@gmail.com> (raw)
In-Reply-To: <20261008142604.39107-2-gnoack3000@gmail.com>

Several tests do not close file descriptors they open: the /dev/null FD
in restrict_self_fd and restrict_self_fd_flags, and the parent's ends
of the synchronization pipes in various ptrace, audit, signal and
abstract UNIX socket tests.

Close them once they are not needed anymore.

Assisted-by: LLM
Signed-off-by: Günther Noack <gnoack3000@gmail.com>
---
 tools/testing/selftests/landlock/audit_test.c          |  2 ++
 tools/testing/selftests/landlock/base_test.c           |  4 ++++
 tools/testing/selftests/landlock/ptrace_test.c         |  4 ++++
 .../selftests/landlock/scoped_abstract_unix_test.c     | 10 ++++++++++
 tools/testing/selftests/landlock/scoped_signal_test.c  |  1 +
 5 files changed, 21 insertions(+)

diff --git a/tools/testing/selftests/landlock/audit_test.c b/tools/testing/selftests/landlock/audit_test.c
index 325fbb9ca297..b38d0c5d5869 100644
--- a/tools/testing/selftests/landlock/audit_test.c
+++ b/tools/testing/selftests/landlock/audit_test.c
@@ -977,6 +977,8 @@ TEST_F(audit_exec, signal_and_open)
 
 	/* Waits for the child to terminate. */
 	EXPECT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
 	ASSERT_EQ(0, WEXITSTATUS(status));
diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
index 4e83f9c52a6f..e486e557f453 100644
--- a/tools/testing/selftests/landlock/base_test.c
+++ b/tools/testing/selftests/landlock/base_test.c
@@ -333,6 +333,8 @@ TEST(restrict_self_fd)
 
 	EXPECT_EQ(-1, landlock_restrict_self(fd, 0));
 	EXPECT_EQ(EBADFD, errno);
+
+	EXPECT_EQ(0, close(fd));
 }
 
 TEST(restrict_self_fd_flags)
@@ -354,6 +356,8 @@ TEST(restrict_self_fd_flags)
 	EXPECT_EQ(-1, landlock_restrict_self(
 			      fd, LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS));
 	EXPECT_EQ(EBADFD, errno);
+
+	EXPECT_EQ(0, close(fd));
 }
 
 TEST(restrict_self_flags)
diff --git a/tools/testing/selftests/landlock/ptrace_test.c b/tools/testing/selftests/landlock/ptrace_test.c
index eb36d4d1ceaf..4fd2b440aa45 100644
--- a/tools/testing/selftests/landlock/ptrace_test.c
+++ b/tools/testing/selftests/landlock/ptrace_test.c
@@ -288,6 +288,8 @@ TEST_F(scoped_domains, trace)
 
 	/* Signals that the parent PTRACE_ATTACH test is done. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 
 	if (WIFSIGNALED(status) || !WIFEXITED(status) ||
@@ -422,6 +424,8 @@ TEST_F(audit, trace)
 
 	/* Signals that the parent PTRACE_ATTACH test is done. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	if (WIFSIGNALED(status) || !WIFEXITED(status) ||
 	    WEXITSTATUS(status) != EXIT_SUCCESS)
diff --git a/tools/testing/selftests/landlock/scoped_abstract_unix_test.c b/tools/testing/selftests/landlock/scoped_abstract_unix_test.c
index 67fcc3380238..df41da5f2ab0 100644
--- a/tools/testing/selftests/landlock/scoped_abstract_unix_test.c
+++ b/tools/testing/selftests/landlock/scoped_abstract_unix_test.c
@@ -156,6 +156,7 @@ TEST_F(scoped_domains, connect_to_parent)
 
 	/* Signals to child that the parent is listening. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	EXPECT_EQ(0, close(stream_server));
@@ -263,6 +264,8 @@ TEST_F(scoped_domains, connect_to_child)
 		EXPECT_EQ(EPERM, errno_dgram);
 	}
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	EXPECT_EQ(0, close(stream_client));
 	EXPECT_EQ(0, close(dgram_client));
 
@@ -433,6 +436,8 @@ TEST_F(scoped_audit, connect_to_child)
 	EXPECT_EQ(0, records.access);
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	EXPECT_EQ(0, close(dgram_client));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
@@ -637,6 +642,7 @@ TEST_F(scoped_vs_unscoped, unix_scoping)
 	ASSERT_EQ(0, listen(stream_server_parent, backlog));
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	EXPECT_EQ(0, close(stream_server_parent));
 	EXPECT_EQ(0, close(dgram_server_parent));
@@ -779,6 +785,7 @@ TEST_F(outside_socket, socket_with_different_domain)
 	} else {
 		server_socket = socket(AF_UNIX, variant->type, 0);
 	}
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_LE(0, server_socket);
 
 	/* Server always has a domain. */
@@ -791,6 +798,7 @@ TEST_F(outside_socket, socket_with_different_domain)
 
 	/* Signals to child that the parent is listening. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	EXPECT_EQ(0, close(server_socket));
@@ -1139,6 +1147,8 @@ TEST(datagram_sockets)
 	 */
 	ASSERT_EQ(1, read(pipe_child[0], &buf, 1));
 	ASSERT_EQ(1, recv(server_conn_socket, &buf, 1, 0));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 
 	/* Waits for all tests to finish. */
 	ASSERT_EQ(child, waitpid(child, &status, 0));
diff --git a/tools/testing/selftests/landlock/scoped_signal_test.c b/tools/testing/selftests/landlock/scoped_signal_test.c
index 47307dd2e242..0f88a6af9c72 100644
--- a/tools/testing/selftests/landlock/scoped_signal_test.c
+++ b/tools/testing/selftests/landlock/scoped_signal_test.c
@@ -1120,6 +1120,7 @@ TEST_F(trace_fown, deny_scope_fown)
 	ASSERT_EQ(0, fcntl(recv_socket, F_SETOWN, child));
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 
 	/* Waits for the child to send MSG_OOB. */
 	ASSERT_EQ(1, read(pipe_child[0], &buffer_parent, 1));
-- 
2.56.0


  parent reply	other threads:[~2026-10-08 14:27 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-08 14:25 [PATCH 00/27] landlock: Selftest fixes and cleanups Günther Noack
2026-10-08 14:25 ` [PATCH 01/27] selftests/landlock: Propagate grandchild failures in unix_scoping Günther Noack
2026-10-08 14:25 ` [PATCH 02/27] selftests/landlock: Propagate EXPECT failures from forked children Günther Noack
2026-10-08 14:25 ` [PATCH 03/27] selftests/landlock: Check the thread result in audit.thread Günther Noack
2026-10-08 14:25 ` [PATCH 04/27] selftests/landlock: Zero-initialize the buffer in inconsistent_attr Günther Noack
2026-10-08 14:25 ` [PATCH 05/27] selftests/landlock: Fix the openat() success check in fs_bench Günther Noack
2026-10-08 14:25 ` [PATCH 06/27] selftests/landlock: Fix the reported clock tick rate " Günther Noack
2026-10-08 14:25 ` [PATCH 07/27] selftests/landlock: Fix the ruleset_fd check in empty_or_same_ruleset Günther Noack
2026-10-08 14:25 ` [PATCH 08/27] selftests/landlock: Handle a missing control message in recv_fd() Günther Noack
2026-10-08 14:25 ` [PATCH 09/27] selftests/landlock: Assert tracefs_extract_field() results that are used Günther Noack
2026-10-08 14:25 ` [PATCH 10/27] selftests/landlock: Assert audit initialization in fixture setups Günther Noack
2026-10-08 14:25 ` [PATCH 11/27] selftests/landlock: Assert helper results that are used afterwards Günther Noack
2026-10-08 14:25 ` [PATCH 12/27] selftests/landlock: Assert audit_count_records() before using its result Günther Noack
2026-10-08 14:25 ` [PATCH 13/27] selftests/landlock: Fix memory leak in audit_init_filter_exe() Günther Noack
2026-10-08 14:25 ` [PATCH 14/27] selftests/landlock: Make audit_message large enough for any exe filter Günther Noack
2026-10-08 14:25 ` Günther Noack [this message]
2026-10-08 14:25 ` [PATCH 16/27] selftests/landlock: Check errno for combined VERSION and ERRATA flags Günther Noack
2026-10-08 14:25 ` [PATCH 17/27] selftests/landlock: Fix SIGURG handler setup in scoped_signal_test Günther Noack
2026-10-08 14:25 ` [PATCH 18/27] selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra() Günther Noack
2026-10-08 14:25 ` [PATCH 19/27] selftests/landlock: Fix off-by-one in regex_escape() Günther Noack
2026-10-08 14:25 ` [PATCH 20/27] selftests/landlock: Fix the snprintf() error check in matches_log_fs_extra() Günther Noack
2026-10-08 14:25 ` [PATCH 21/27] selftests/landlock: Use _exit() in the named_pipe_ioctl child Günther Noack
2026-10-08 14:25 ` [PATCH 22/27] selftests/landlock: Fix messages and comments in helper programs Günther Noack
2026-10-08 14:25 ` [PATCH 23/27] selftests/landlock: Fix stale comments in common.h and audit.h Günther Noack
2026-10-08 14:25 ` [PATCH 24/27] selftests/landlock: Use ASSERT for syscall failures before errno checks Günther Noack
2026-10-08 14:25 ` [PATCH 25/27] selftests/landlock: Assert WIFEXITED() before checking the exit status Günther Noack
2026-10-08 14:25 ` [PATCH 26/27] selftests/landlock: Use EXPECT for closing FDs and stopping threads Günther Noack
2026-10-08 14:25 ` [PATCH 27/27] selftests/landlock: Use EXPECT for checks that later steps do not need Günther Noack
2026-10-08 14:33 ` [PATCH 00/27] landlock: Selftest fixes and cleanups Günther Noack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261008142604.39107-17-gnoack3000@gmail.com \
    --to=gnoack3000@gmail.com \
    --cc=gnoack@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=shuah@kernel.org \
    --cc=wangyan01@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox