[v2,4/4] socket: Add a test for MSG_CMSG_CLOEXEC

Message ID 20230423160548.126576-4-bugaevc@gmail.com
State Committed, archived
Headers
Series [v2,1/4] hurd: Don't pass FD_CLOEXEC in CMSG_DATA |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Sergey Bugaev April 23, 2023, 4:05 p.m. UTC
  This checks that:
* We can send and receive fds over Unix domain sockets using SCM_RIGHTS;
* msg_controllen, cmsg_level, cmsg_type, cmsg_len are all filled in
  correctly on receive;
* Most importantly, the received fd has or has not the close-on-exec
  flag set depending on whether we pass MSG_CMSG_CLOEXEC to recvmsg ().

Checked on i686-gnu and x86_64-linux-gnu.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 socket/Makefile           |   1 +
 socket/tst-cmsg_cloexec.c | 126 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+)
 create mode 100644 socket/tst-cmsg_cloexec.c
  

Comments

Samuel Thibault April 24, 2023, 10:21 p.m. UTC | #1
Applied, thanks!

Sergey Bugaev, le dim. 23 avril 2023 19:05:48 +0300, a ecrit:
> This checks that:
> * We can send and receive fds over Unix domain sockets using SCM_RIGHTS;
> * msg_controllen, cmsg_level, cmsg_type, cmsg_len are all filled in
>   correctly on receive;
> * Most importantly, the received fd has or has not the close-on-exec
>   flag set depending on whether we pass MSG_CMSG_CLOEXEC to recvmsg ().
> 
> Checked on i686-gnu and x86_64-linux-gnu.
> 
> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  socket/Makefile           |   1 +
>  socket/tst-cmsg_cloexec.c | 126 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 127 insertions(+)
>  create mode 100644 socket/tst-cmsg_cloexec.c
> 
> diff --git a/socket/Makefile b/socket/Makefile
> index fffed7dd..94951ae3 100644
> --- a/socket/Makefile
> +++ b/socket/Makefile
> @@ -35,6 +35,7 @@ tests := \
>    tst-accept4 \
>    tst-sockopt \
>    tst-cmsghdr \
> +  tst-cmsg_cloexec \
>    # tests
>  
>  tests-internal := \
> diff --git a/socket/tst-cmsg_cloexec.c b/socket/tst-cmsg_cloexec.c
> new file mode 100644
> index 00000000..a18f4912
> --- /dev/null
> +++ b/socket/tst-cmsg_cloexec.c
> @@ -0,0 +1,126 @@
> +/* Smoke test for MSG_CMSG_CLOEXEC.
> +   Copyright (C) 2021-2023 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xunistd.h>
> +#include <support/check.h>
> +#include <sys/socket.h>
> +#include <sys/un.h>
> +#include <string.h>
> +#include <fcntl.h>
> +
> +static void
> +send_fd (int sockfd, int fd)
> +{
> +  char data[] = "hello";
> +  struct iovec iov = { .iov_base = data, .iov_len = sizeof (data) };
> +
> +  union
> +  {
> +    struct cmsghdr header;
> +    char bytes[CMSG_SPACE (sizeof (fd))];
> +  } cmsg_storage;
> +
> +  struct msghdr msg =
> +    {
> +      .msg_iov = &iov,
> +      .msg_iovlen = 1,
> +      .msg_control = cmsg_storage.bytes,
> +      .msg_controllen = sizeof (cmsg_storage)
> +    };
> +
> +  memset (&cmsg_storage, 0, sizeof (cmsg_storage));
> +
> +  struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
> +  cmsg->cmsg_level = SOL_SOCKET;
> +  cmsg->cmsg_type = SCM_RIGHTS;
> +  cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
> +  memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
> +
> +  ssize_t nsent = sendmsg (sockfd, &msg, 0);
> +  if (nsent < 0)
> +    FAIL_EXIT1 ("sendmsg (%d): %m", sockfd);
> +  TEST_COMPARE (nsent, sizeof (data));
> +}
> +
> +static int
> +recv_fd (int sockfd, int flags)
> +{
> +  char buffer[100];
> +  struct iovec iov = { .iov_base = buffer, .iov_len = sizeof (buffer) };
> +
> +  union
> +  {
> +    struct cmsghdr header;
> +    char bytes[100];
> +  } cmsg_storage;
> +
> +  struct msghdr msg =
> +    {
> +      .msg_iov = &iov,
> +      .msg_iovlen = 1,
> +      .msg_control = cmsg_storage.bytes,
> +      .msg_controllen = sizeof (cmsg_storage)
> +    };
> +
> +  ssize_t nrecv = recvmsg (sockfd, &msg, flags);
> +  if (nrecv < 0)
> +    FAIL_EXIT1 ("recvmsg (%d): %m", sockfd);
> +
> +  TEST_COMPARE (msg.msg_controllen, CMSG_SPACE (sizeof (int)));
> +  struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
> +  TEST_COMPARE (cmsg->cmsg_level, SOL_SOCKET);
> +  TEST_COMPARE (cmsg->cmsg_type, SCM_RIGHTS);
> +  TEST_COMPARE (cmsg->cmsg_len, CMSG_LEN (sizeof (int)));
> +
> +  int fd;
> +  memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
> +  return fd;
> +}
> +
> +static int
> +do_test (void)
> +{
> +  int sockfd[2];
> +  int newfd;
> +  int flags;
> +  int rc = socketpair (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockfd);
> +  if (rc < 0)
> +    FAIL_EXIT1 ("socketpair: %m");
> +
> +  send_fd (sockfd[1], STDIN_FILENO);
> +  newfd = recv_fd (sockfd[0], 0);
> +  TEST_VERIFY_EXIT (newfd > 0);
> +  flags = fcntl (newfd, F_GETFD, 0);
> +  TEST_VERIFY_EXIT (flags != -1);
> +  TEST_VERIFY (!(flags & FD_CLOEXEC));
> +  xclose (newfd);
> +
> +  send_fd (sockfd[1], STDIN_FILENO);
> +  newfd = recv_fd (sockfd[0], MSG_CMSG_CLOEXEC);
> +  TEST_VERIFY_EXIT (newfd > 0);
> +  flags = fcntl (newfd, F_GETFD, 0);
> +  TEST_VERIFY_EXIT (flags != -1);
> +  TEST_VERIFY (flags & FD_CLOEXEC);
> +  xclose (newfd);
> +
> +  xclose (sockfd[0]);
> +  xclose (sockfd[1]);
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>
> -- 
> 2.40.0
>
  

Patch

diff --git a/socket/Makefile b/socket/Makefile
index fffed7dd..94951ae3 100644
--- a/socket/Makefile
+++ b/socket/Makefile
@@ -35,6 +35,7 @@  tests := \
   tst-accept4 \
   tst-sockopt \
   tst-cmsghdr \
+  tst-cmsg_cloexec \
   # tests
 
 tests-internal := \
diff --git a/socket/tst-cmsg_cloexec.c b/socket/tst-cmsg_cloexec.c
new file mode 100644
index 00000000..a18f4912
--- /dev/null
+++ b/socket/tst-cmsg_cloexec.c
@@ -0,0 +1,126 @@ 
+/* Smoke test for MSG_CMSG_CLOEXEC.
+   Copyright (C) 2021-2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xunistd.h>
+#include <support/check.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <string.h>
+#include <fcntl.h>
+
+static void
+send_fd (int sockfd, int fd)
+{
+  char data[] = "hello";
+  struct iovec iov = { .iov_base = data, .iov_len = sizeof (data) };
+
+  union
+  {
+    struct cmsghdr header;
+    char bytes[CMSG_SPACE (sizeof (fd))];
+  } cmsg_storage;
+
+  struct msghdr msg =
+    {
+      .msg_iov = &iov,
+      .msg_iovlen = 1,
+      .msg_control = cmsg_storage.bytes,
+      .msg_controllen = sizeof (cmsg_storage)
+    };
+
+  memset (&cmsg_storage, 0, sizeof (cmsg_storage));
+
+  struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = SCM_RIGHTS;
+  cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
+  memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
+
+  ssize_t nsent = sendmsg (sockfd, &msg, 0);
+  if (nsent < 0)
+    FAIL_EXIT1 ("sendmsg (%d): %m", sockfd);
+  TEST_COMPARE (nsent, sizeof (data));
+}
+
+static int
+recv_fd (int sockfd, int flags)
+{
+  char buffer[100];
+  struct iovec iov = { .iov_base = buffer, .iov_len = sizeof (buffer) };
+
+  union
+  {
+    struct cmsghdr header;
+    char bytes[100];
+  } cmsg_storage;
+
+  struct msghdr msg =
+    {
+      .msg_iov = &iov,
+      .msg_iovlen = 1,
+      .msg_control = cmsg_storage.bytes,
+      .msg_controllen = sizeof (cmsg_storage)
+    };
+
+  ssize_t nrecv = recvmsg (sockfd, &msg, flags);
+  if (nrecv < 0)
+    FAIL_EXIT1 ("recvmsg (%d): %m", sockfd);
+
+  TEST_COMPARE (msg.msg_controllen, CMSG_SPACE (sizeof (int)));
+  struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+  TEST_COMPARE (cmsg->cmsg_level, SOL_SOCKET);
+  TEST_COMPARE (cmsg->cmsg_type, SCM_RIGHTS);
+  TEST_COMPARE (cmsg->cmsg_len, CMSG_LEN (sizeof (int)));
+
+  int fd;
+  memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
+  return fd;
+}
+
+static int
+do_test (void)
+{
+  int sockfd[2];
+  int newfd;
+  int flags;
+  int rc = socketpair (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockfd);
+  if (rc < 0)
+    FAIL_EXIT1 ("socketpair: %m");
+
+  send_fd (sockfd[1], STDIN_FILENO);
+  newfd = recv_fd (sockfd[0], 0);
+  TEST_VERIFY_EXIT (newfd > 0);
+  flags = fcntl (newfd, F_GETFD, 0);
+  TEST_VERIFY_EXIT (flags != -1);
+  TEST_VERIFY (!(flags & FD_CLOEXEC));
+  xclose (newfd);
+
+  send_fd (sockfd[1], STDIN_FILENO);
+  newfd = recv_fd (sockfd[0], MSG_CMSG_CLOEXEC);
+  TEST_VERIFY_EXIT (newfd > 0);
+  flags = fcntl (newfd, F_GETFD, 0);
+  TEST_VERIFY_EXIT (flags != -1);
+  TEST_VERIFY (flags & FD_CLOEXEC);
+  xclose (newfd);
+
+  xclose (sockfd[0]);
+  xclose (sockfd[1]);
+  return 0;
+}
+
+#include <support/test-driver.c>