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:08:09 +0000 (10:08 -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 5b9cb85381fcb815998c7ecaa733a52a7d2a31e7..814ccde17954b82200242e4ec03304907c2fac5c 100644 (file)
@@ -107,6 +107,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) {
@@ -452,6 +453,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));
 
@@ -507,7 +509,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 44a830af20446142f8e21177b5187e8dfd6d4af0..461befb99db5ed45cde5dcdcfecb860df06003b3 100644 (file)
@@ -329,11 +329,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.027045 seconds and 4 git commands to generate.