]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/af_unix.c
confile: cleanup parse_line()
[mirror_lxc.git] / src / lxc / af_unix.c
index e6ba81fb89006ea20461fc210b83e5edc0ad4e6d..bd29b09da07134d6977d627bd975e63a2465ade4 100644 (file)
-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <daniel.lezcano at free.fr>
- *
- * This 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.
- *
- * This 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 this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-#include "config.h"
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stddef.h>
 #include <string.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
 #include <sys/socket.h>
+#include <sys/syscall.h>
 #include <sys/un.h>
 
+#include "af_unix.h"
+#include "config.h"
 #include "log.h"
+#include "macro.h"
+#include "memory_utils.h"
+#include "process_utils.h"
+#include "utils.h"
 
-lxc_log_define(lxc_af_unix, lxc);
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
 
-int lxc_abstract_unix_open(const char *path, int type, int flags)
+lxc_log_define(af_unix, lxc);
+
+static ssize_t lxc_abstract_unix_set_sockaddr(struct sockaddr_un *addr,
+                                             const char *path)
 {
-       int fd;
        size_t len;
-       struct sockaddr_un addr;
-
-       if (flags & O_TRUNC)
-               unlink(path);
 
-       fd = socket(PF_UNIX, type, 0);
-       if (fd < 0)
-               return -1;
+       if (!addr || !path)
+               return ret_errno(EINVAL);
 
        /* Clear address structure */
-       memset(&addr, 0, sizeof(addr));
-
-       if (!path)
-               return fd;
+       memset(addr, 0, sizeof(*addr));
 
-       addr.sun_family = AF_UNIX;
+       addr->sun_family = AF_UNIX;
 
        len = strlen(&path[1]);
+
+       /* do not enforce \0-termination */
+       if (len >= INT_MAX || len >= sizeof(addr->sun_path))
+               return ret_errno(ENAMETOOLONG);
+
        /* do not enforce \0-termination */
-       if (len >= sizeof(addr.sun_path)) {
-               close(fd);
-               errno = ENAMETOOLONG;
+       memcpy(&addr->sun_path[1], &path[1], len);
+       return len;
+}
+
+int lxc_abstract_unix_open(const char *path, int type, int flags)
+{
+       __do_close int fd = -EBADF;
+       int ret;
+       ssize_t len;
+       struct sockaddr_un addr;
+
+       fd = socket(PF_UNIX, type | SOCK_CLOEXEC, 0);
+       if (fd < 0)
                return -1;
-       }
-       /* addr.sun_path[0] has already been set to 0 by memset() */
-       strncpy(&addr.sun_path[1], &path[1], strlen(&path[1]));
 
-       if (bind(fd, (struct sockaddr *)&addr, offsetof(struct sockaddr_un, sun_path) + len + 1)) {
-               int tmp = errno;
-               close(fd);
-               errno = tmp;
+       if (!path)
+               return move_fd(fd);
+
+       len = lxc_abstract_unix_set_sockaddr(&addr, path);
+       if (len < 0)
                return -1;
-       }
 
-       if (type == SOCK_STREAM && listen(fd, 100)) {
-               int tmp = errno;
-               close(fd);
-               errno = tmp;
+       ret = bind(fd, (struct sockaddr *)&addr,
+                  offsetof(struct sockaddr_un, sun_path) + len + 1);
+       if (ret < 0)
                return -1;
+
+       if (type == SOCK_STREAM) {
+               ret = listen(fd, 100);
+               if (ret < 0)
+                       return -1;
        }
 
-       return fd;
+       return move_fd(fd);
 }
 
-int lxc_abstract_unix_close(int fd)
+void lxc_abstract_unix_close(int fd)
 {
-       struct sockaddr_un addr;
-       socklen_t addrlen = sizeof(addr);
-
-       if (!getsockname(fd, (struct sockaddr *)&addr, &addrlen) &&
-                       addr.sun_path[0])
-               unlink(addr.sun_path);
-
        close(fd);
-
-       return 0;
 }
 
 int lxc_abstract_unix_connect(const char *path)
 {
-       int fd;
-       size_t len;
+       __do_close int fd = -EBADF;
+       int ret;
+       ssize_t len;
        struct sockaddr_un addr;
 
-       fd = socket(PF_UNIX, SOCK_STREAM, 0);
+       fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
        if (fd < 0)
                return -1;
 
-       memset(&addr, 0, sizeof(addr));
-
-       addr.sun_family = AF_UNIX;
-
-       len = strlen(&path[1]);
-       /* do not enforce \0-termination */
-       if (len >= sizeof(addr.sun_path)) {
-               close(fd);
-               errno = ENAMETOOLONG;
+       len = lxc_abstract_unix_set_sockaddr(&addr, path);
+       if (len < 0)
                return -1;
-       }
-       /* addr.sun_path[0] has already been set to 0 by memset() */
-       strncpy(&addr.sun_path[1], &path[1], strlen(&path[1]));
 
-       if (connect(fd, (struct sockaddr *)&addr, offsetof(struct sockaddr_un, sun_path) + len + 1)) {
-               close(fd);
+       ret = connect(fd, (struct sockaddr *)&addr,
+                     offsetof(struct sockaddr_un, sun_path) + len + 1);
+       if (ret < 0)
                return -1;
-       }
 
-       return fd;
+       return move_fd(fd);
 }
 
-int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds,
-                              void *data, size_t size)
+int lxc_abstract_unix_send_fds_iov(int fd, int *sendfds, int num_sendfds,
+                                  struct iovec *iov, size_t iovlen)
 {
+       __do_free char *cmsgbuf = NULL;
        int ret;
        struct msghdr msg;
-       struct iovec iov;
        struct cmsghdr *cmsg = NULL;
-       char buf[1] = {0};
-       char *cmsgbuf;
        size_t cmsgbufsize = CMSG_SPACE(num_sendfds * sizeof(int));
 
        memset(&msg, 0, sizeof(msg));
-       memset(&iov, 0, sizeof(iov));
 
        cmsgbuf = malloc(cmsgbufsize);
-       if (!cmsgbuf)
+       if (!cmsgbuf) {
+               errno = ENOMEM;
                return -1;
+       }
 
        msg.msg_control = cmsgbuf;
        msg.msg_controllen = cmsgbufsize;
@@ -160,66 +141,96 @@ int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds,
 
        memcpy(CMSG_DATA(cmsg), sendfds, num_sendfds * sizeof(int));
 
-       iov.iov_base = data ? data : buf;
-       iov.iov_len = data ? size : sizeof(buf);
-       msg.msg_iov = &iov;
-       msg.msg_iovlen = 1;
+       msg.msg_iov = iov;
+       msg.msg_iovlen = iovlen;
+
+       do {
+               ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
+       } while (ret < 0 && errno == EINTR);
 
-       ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
-       free(cmsgbuf);
        return ret;
 }
 
-int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds,
+int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds,
                               void *data, size_t size)
 {
+       char buf[1] = {0};
+       struct iovec iov = {
+               .iov_base = data ? data : buf,
+               .iov_len = data ? size : sizeof(buf),
+       };
+       return lxc_abstract_unix_send_fds_iov(fd, sendfds, num_sendfds, &iov, 1);
+}
+
+int lxc_unix_send_fds(int fd, int *sendfds, int num_sendfds, void *data,
+                     size_t size)
+{
+       return lxc_abstract_unix_send_fds(fd, sendfds, num_sendfds, data, size);
+}
+
+static int lxc_abstract_unix_recv_fds_iov(int fd, int *recvfds, int num_recvfds,
+                                         struct iovec *iov, size_t iovlen)
+{
+       __do_free char *cmsgbuf = NULL;
        int ret;
        struct msghdr msg;
-       struct iovec iov;
-       struct cmsghdr *cmsg = NULL;
-       char buf[1] = {0};
-       char *cmsgbuf;
-       size_t cmsgbufsize = CMSG_SPACE(num_recvfds * sizeof(int));
+       size_t cmsgbufsize = CMSG_SPACE(sizeof(struct ucred)) +
+                            CMSG_SPACE(num_recvfds * sizeof(int));
 
        memset(&msg, 0, sizeof(msg));
-       memset(&iov, 0, sizeof(iov));
 
        cmsgbuf = malloc(cmsgbufsize);
        if (!cmsgbuf)
-               return -1;
+               return ret_errno(ENOMEM);
 
        msg.msg_control = cmsgbuf;
        msg.msg_controllen = cmsgbufsize;
 
-       iov.iov_base = data ? data : buf;
-       iov.iov_len = data ? size : sizeof(buf);
-       msg.msg_iov = &iov;
-       msg.msg_iovlen = 1;
-
-       ret = recvmsg(fd, &msg, 0);
-       if (ret <= 0)
-               goto out;
-
-       cmsg = CMSG_FIRSTHDR(&msg);
-
-       memset(recvfds, -1, num_recvfds * sizeof(int));
-       if (cmsg && cmsg->cmsg_len == CMSG_LEN(num_recvfds * sizeof(int)) &&
-           cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
-               memcpy(recvfds, CMSG_DATA(cmsg), num_recvfds * sizeof(int));
+       msg.msg_iov = iov;
+       msg.msg_iovlen = iovlen;
+
+       do {
+               ret = recvmsg(fd, &msg, MSG_CMSG_CLOEXEC);
+       } while (ret < 0 && errno == EINTR);
+       if (ret < 0 || ret == 0)
+               return ret;
+
+       /*
+        * If SO_PASSCRED is set we will always get a ucred message.
+        */
+       for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+               if (cmsg->cmsg_type != SCM_RIGHTS)
+                       continue;
+
+               memset(recvfds, -1, num_recvfds * sizeof(int));
+               if (cmsg &&
+                   cmsg->cmsg_len == CMSG_LEN(num_recvfds * sizeof(int)) &&
+                   cmsg->cmsg_level == SOL_SOCKET)
+                       memcpy(recvfds, CMSG_DATA(cmsg), num_recvfds * sizeof(int));
+               break;
        }
 
-out:
-       free(cmsgbuf);
        return ret;
 }
 
+int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds,
+                              void *data, size_t size)
+{
+       char buf[1] = {0};
+       struct iovec iov = {
+               .iov_base = data ? data : buf,
+               .iov_len = data ? size : sizeof(buf),
+       };
+       return lxc_abstract_unix_recv_fds_iov(fd, recvfds, num_recvfds, &iov, 1);
+}
+
 int lxc_abstract_unix_send_credential(int fd, void *data, size_t size)
 {
-       struct msghdr msg = { 0 };
+       struct msghdr msg = {0};
        struct iovec iov;
        struct cmsghdr *cmsg;
        struct ucred cred = {
-               .pid = getpid(),
+               .pid = lxc_raw_getpid(),
                .uid = getuid(),
                .gid = getgid(),
        };
@@ -248,7 +259,7 @@ int lxc_abstract_unix_send_credential(int fd, void *data, size_t size)
 
 int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size)
 {
-       struct msghdr msg = { 0 };
+       struct msghdr msg = {0};
        struct iovec iov;
        struct cmsghdr *cmsg;
        struct ucred cred;
@@ -268,19 +279,98 @@ int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size)
 
        ret = recvmsg(fd, &msg, 0);
        if (ret <= 0)
-               goto out;
+               return ret;
 
        cmsg = CMSG_FIRSTHDR(&msg);
 
        if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) &&
-                       cmsg->cmsg_level == SOL_SOCKET &&
-                       cmsg->cmsg_type == SCM_CREDENTIALS) {
+           cmsg->cmsg_level == SOL_SOCKET &&
+           cmsg->cmsg_type == SCM_CREDENTIALS) {
                memcpy(&cred, CMSG_DATA(cmsg), sizeof(cred));
-               if (cred.uid && (cred.uid != getuid() || cred.gid != getgid())) {
-                       INFO("message denied for '%d/%d'", cred.uid, cred.gid);
-                       return -EACCES;
-               }
+
+               if (cred.uid && (cred.uid != getuid() || cred.gid != getgid()))
+                       return log_error_errno(-1, EACCES,
+                                              "Message denied for '%d/%d'",
+                                              cred.uid, cred.gid);
        }
-out:
+
        return ret;
 }
+
+int lxc_unix_sockaddr(struct sockaddr_un *ret, const char *path)
+{
+       size_t len;
+
+       len = strlen(path);
+       if (len == 0)
+               return ret_set_errno(-1, EINVAL);
+       if (path[0] != '/' && path[0] != '@')
+               return ret_set_errno(-1, EINVAL);
+       if (path[1] == '\0')
+               return ret_set_errno(-1, EINVAL);
+
+       if (len + 1 > sizeof(ret->sun_path))
+               return ret_set_errno(-1, EINVAL);
+
+       *ret = (struct sockaddr_un){
+           .sun_family = AF_UNIX,
+       };
+
+       if (path[0] == '@') {
+               memcpy(ret->sun_path + 1, path + 1, len);
+               return (int)(offsetof(struct sockaddr_un, sun_path) + len);
+       }
+
+       memcpy(ret->sun_path, path, len + 1);
+       return (int)(offsetof(struct sockaddr_un, sun_path) + len + 1);
+}
+
+int lxc_unix_connect_type(struct sockaddr_un *addr, int type)
+{
+       __do_close int fd = -EBADF;
+       int ret;
+       ssize_t len;
+
+       fd = socket(AF_UNIX, type | SOCK_CLOEXEC, 0);
+       if (fd < 0)
+               return log_error_errno(-1, errno,
+                                      "Failed to open new AF_UNIX socket");
+
+       if (addr->sun_path[0] == '\0')
+               len = strlen(&addr->sun_path[1]);
+       else
+               len = strlen(&addr->sun_path[0]);
+
+       ret = connect(fd, (struct sockaddr *)addr,
+                     offsetof(struct sockaddr_un, sun_path) + len);
+       if (ret < 0)
+               return log_error_errno(-1, errno,
+                                      "Failed to bind new AF_UNIX socket");
+
+       return move_fd(fd);
+}
+
+int lxc_unix_connect(struct sockaddr_un *addr)
+{
+       return lxc_unix_connect_type(addr, SOCK_STREAM);
+}
+
+int lxc_socket_set_timeout(int fd, int rcv_timeout, int snd_timeout)
+{
+       struct timeval out = {0};
+       int ret;
+
+       out.tv_sec = snd_timeout;
+       ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const void *)&out,
+                        sizeof(out));
+       if (ret < 0)
+               return -1;
+
+       out.tv_sec = rcv_timeout;
+       ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const void *)&out,
+                        sizeof(out));
+       if (ret < 0)
+               return -1;
+
+       return 0;
+}