Fix: report an error if unix socket address is too long
[lttng-tools.git] / src / common / sessiond-comm / unix.c
index 4b6416168afa9f465406dd4bcce73d5d129180fe..39ac89d53281804d33598d2f08538480108b4c30 100644 (file)
@@ -41,6 +41,14 @@ int lttcomm_connect_unix_sock(const char *pathname)
        struct sockaddr_un sun;
        int fd, ret, closeret;
 
+       if (strlen(pathname) >= sizeof(sun.sun_path)) {
+               ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
+                               pathname, strlen(pathname) + 1,
+                               sizeof(sun.sun_path));
+               ret = -ENAMETOOLONG;
+               goto error;
+       }
+
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd < 0) {
                PERROR("socket");
@@ -111,9 +119,17 @@ LTTNG_HIDDEN
 int lttcomm_create_unix_sock(const char *pathname)
 {
        struct sockaddr_un sun;
-       int fd;
+       int fd = -1;
        int ret = -1;
 
+       if (strlen(pathname) >= sizeof(sun.sun_path)) {
+               ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
+                               pathname, strlen(pathname) + 1,
+                               sizeof(sun.sun_path));
+               ret = -ENAMETOOLONG;
+               goto error;
+       }
+
        /* Create server socket */
        if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
                PERROR("socket");
@@ -281,6 +297,9 @@ ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
        msg.msg_controllen = CMSG_LEN(sizeof_fds);
 
        cmptr = CMSG_FIRSTHDR(&msg);
+       if (!cmptr) {
+               return -1;
+       }
        cmptr->cmsg_level = SOL_SOCKET;
        cmptr->cmsg_type = SCM_RIGHTS;
        cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
@@ -408,6 +427,9 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
        msg.msg_controllen = CMSG_LEN(sizeof_cred);
 
        cmptr = CMSG_FIRSTHDR(&msg);
+       if (!cmptr) {
+               return -1;
+       }
        cmptr->cmsg_level = SOL_SOCKET;
        cmptr->cmsg_type = LTTNG_SOCK_CREDS;
        cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
This page took 0.023852 seconds and 4 git commands to generate.