Fix: set FD_CLOEXEC on incoming FDs.
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Mon, 2 Mar 2020 19:21:33 +0000 (14:21 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 4 Mar 2020 15:10:24 +0000 (10:10 -0500)
The stream shm FDs are allocated by the consumer process, and then
passed to the applications over unix sockets. When opening those
file descriptors on reception, the FD_CLOEXEC flag is not set.

In a fork + exec scenario, parent process streams shm FDs and channel
wake FDs are present in the resulting child process.

Set FD_CLOEXEC on reception (ustcomm_recv_fds_unix_sock) to
prevent such scenario.

Change-Id: Id58077b272be9c1ab239846639ffd8103b3d50f1
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-comm/lttng-ust-comm.c
libringbuffer/shm.c

index beeee60b9b7a02b3c63e182d9932db90538c6a05..d80055e9d05f23379dcc078f774d42e8cd77455b 100644 (file)
@@ -106,6 +106,7 @@ int ustcomm_connect_unix_sock(const char *pathname, long timeout)
        /*
         * libust threads require the close-on-exec flag for all
         * resources so it does not leak file descriptors upon exec.
+        * SOCK_CLOEXEC is not used since it is linux specific.
         */
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd < 0) {
@@ -451,6 +452,7 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
        char recv_fd[CMSG_SPACE(sizeof_fds)];
        struct msghdr msg;
        char dummy;
+       int i;
 
        memset(&msg, 0, sizeof(msg));
 
@@ -506,7 +508,18 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
                ret = -1;
                goto end;
        }
+
        memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
+
+       /* Set FD_CLOEXEC */
+       for (i = 0; i < nb_fd; i++) {
+               ret = fcntl(fds[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
+                              fds[i]);
+               }
+       }
+
        ret = nb_fd;
 end:
        return ret;
index ea946ea3f5e2a6d2e46d3297ebe925135164272e..7991d06fef6e93266f15c476e42be23768109958 100644 (file)
@@ -279,11 +279,6 @@ struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
        obj->shm_fd = shm_fd;
        obj->shm_fd_ownership = 1;
 
-       ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC);
-       if (ret < 0) {
-               PERROR("fcntl");
-               goto error_fcntl;
-       }
        /* The write end of the pipe needs to be non-blocking */
        ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK);
        if (ret < 0) {
This page took 0.026121 seconds and 4 git commands to generate.