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 <[email protected]>
---
 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


Reply via email to