]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
utils: fix recv_creds()
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 16 Apr 2020 15:58:37 +0000 (17:58 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 16 Apr 2020 16:51:41 +0000 (18:51 +0200)
Fixes: Coverity 355704.
Fixes: Coverity 355718.
Fixes: Coverity 355738.
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/bindings.c
src/cgroup_fuse.c
src/utils.c

index 8ae860583c312b830ee38b32780471ce8e8fd7ec..0c1973938e5c83a7a838eb550b51340c3b96d639 100644 (file)
@@ -398,9 +398,13 @@ static pid_t get_init_pid_for_task(pid_t task)
 {
        char v = '0';
        pid_t pid_ret = -1;
+       struct ucred cred = {
+               .pid = -1,
+               .uid = -1,
+               .gid = -1,
+       };
        pid_t pid;
        int sock[2];
-       struct ucred cred;
 
        if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sock) < 0)
                return -1;
index a32ec731eb87d95242e5e8f663ee2edb030437ae..d4bdac139c3f4f6f2cab4c9229bf9c7bd130f200 100644 (file)
@@ -1158,13 +1158,17 @@ out:
 static int pid_to_ns(int sock, pid_t tpid)
 {
        char v = '0';
-       struct ucred cred;
+       struct ucred cred = {
+               .pid = -1,
+               .uid = -1,
+               .gid = -1,
+       };
 
        while (recv_creds(sock, &cred, &v)) {
                if (v == '1')
                        return 0;
 
-               if (write(sock, &cred.pid, sizeof(pid_t)) != sizeof(pid_t))
+               if (write_nointr(sock, &cred.pid, sizeof(pid_t)) != sizeof(pid_t))
                        return 1;
        }
 
index 0be997c2f4d886bf18bdffb704e05a2b5d5f99f1..ef44e3ad011ca3383a1a99f0367c8b5c380b07d7 100644 (file)
@@ -212,37 +212,34 @@ again:
 
 bool recv_creds(int sock, struct ucred *cred, char *v)
 {
-       struct msghdr msg = { 0 };
+       struct msghdr msg = {};
        struct iovec iov;
        struct cmsghdr *cmsg;
-       char cmsgbuf[CMSG_SPACE(sizeof(*cred))];
-       char buf[1];
-       int ret;
+       ssize_t ret;
+       char cmsgbuf[CMSG_SPACE(sizeof(*cred))] = {};
+       char buf = '1';
        int optval = 1;
 
-       *v = '1';
-
-       cred->pid = -1;
-       cred->uid = -1;
-       cred->gid = -1;
-
-       if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1)
-               return log_error(false, "Failed to set passcred: %s\n", strerror(errno));
-
-       buf[0] = '1';
-       if (write(sock, buf, 1) != 1)
-               return log_error(false, "Failed to start write on scm fd: %s\n", strerror(errno));
-
        msg.msg_name = NULL;
        msg.msg_namelen = 0;
        msg.msg_control = cmsgbuf;
        msg.msg_controllen = sizeof(cmsgbuf);
 
-       iov.iov_base = buf;
+       iov.iov_base = &buf;
        iov.iov_len = sizeof(buf);
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
 
+       *v = buf;
+
+       ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
+       if (ret < 0)
+               return log_error(false, "Failed to set passcred: %s\n", strerror(errno));
+
+       ret = write_nointr(sock, &buf, sizeof(buf));
+       if (ret != sizeof(buf))
+               return log_error(false, "Failed to start write on scm fd: %s\n", strerror(errno));
+
        if (!wait_for_sock(sock, 2))
                return log_error(false, "Timed out waiting for scm_cred: %s\n", strerror(errno));
 
@@ -252,12 +249,12 @@ bool recv_creds(int sock, struct ucred *cred, char *v)
 
        cmsg = CMSG_FIRSTHDR(&msg);
 
-       if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) &&
-                       cmsg->cmsg_level == SOL_SOCKET &&
-                       cmsg->cmsg_type == SCM_CREDENTIALS) {
+       if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(*cred)) &&
+           cmsg->cmsg_level == SOL_SOCKET &&
+           cmsg->cmsg_type == SCM_CREDENTIALS) {
                memcpy(cred, CMSG_DATA(cmsg), sizeof(*cred));
        }
-       *v = buf[0];
+       *v = buf;
 
        return true;
 }