Blame SOURCES/kvm-vhost-user-support-receiving-file-descriptors-in-sla.patch

1bdc94
From f625d197050d05d33172bd5e93d044155208befb Mon Sep 17 00:00:00 2001
1bdc94
From: "plai@redhat.com" <plai@redhat.com>
1bdc94
Date: Thu, 21 Jun 2018 18:54:38 +0200
1bdc94
Subject: [PATCH 29/57] vhost-user: support receiving file descriptors in
1bdc94
 slave_read
1bdc94
1bdc94
RH-Author: plai@redhat.com
1bdc94
Message-id: <1529607285-9942-4-git-send-email-plai@redhat.com>
1bdc94
Patchwork-id: 80933
1bdc94
O-Subject: [RHEL7.6 PATCH BZ 1526645 03/10] vhost-user: support receiving file descriptors in slave_read
1bdc94
Bugzilla: 1526645
1bdc94
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
1bdc94
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
1bdc94
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
1bdc94
1bdc94
From: Tiwei Bie <tiwei.bie@intel.com>
1bdc94
1bdc94
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
1bdc94
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
1bdc94
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1bdc94
(cherry picked from commit 1f3a4519b1c107b5db2434b30638353978366b4d)
1bdc94
Signed-off-by: Paul Lai <plai@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 hw/virtio/vhost-user.c | 41 ++++++++++++++++++++++++++++++++++++++++-
1bdc94
 1 file changed, 40 insertions(+), 1 deletion(-)
1bdc94
1bdc94
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
1bdc94
index 38da869..85d8fd2 100644
1bdc94
--- a/hw/virtio/vhost-user.c
1bdc94
+++ b/hw/virtio/vhost-user.c
1bdc94
@@ -852,14 +852,44 @@ static void slave_read(void *opaque)
1bdc94
     VhostUserHeader hdr = { 0, };
1bdc94
     VhostUserPayload payload = { 0, };
1bdc94
     int size, ret = 0;
1bdc94
+    struct iovec iov;
1bdc94
+    struct msghdr msgh;
1bdc94
+    int fd = -1;
1bdc94
+    char control[CMSG_SPACE(sizeof(fd))];
1bdc94
+    struct cmsghdr *cmsg;
1bdc94
+    size_t fdsize;
1bdc94
+
1bdc94
+    memset(&msgh, 0, sizeof(msgh));
1bdc94
+    msgh.msg_iov = &iov;
1bdc94
+    msgh.msg_iovlen = 1;
1bdc94
+    msgh.msg_control = control;
1bdc94
+    msgh.msg_controllen = sizeof(control);
1bdc94
 
1bdc94
     /* Read header */
1bdc94
-    size = read(u->slave_fd, &hdr, VHOST_USER_HDR_SIZE);
1bdc94
+    iov.iov_base = &hd;;
1bdc94
+    iov.iov_len = VHOST_USER_HDR_SIZE;
1bdc94
+
1bdc94
+    size = recvmsg(u->slave_fd, &msgh, 0);
1bdc94
     if (size != VHOST_USER_HDR_SIZE) {
1bdc94
         error_report("Failed to read from slave.");
1bdc94
         goto err;
1bdc94
     }
1bdc94
 
1bdc94
+    if (msgh.msg_flags & MSG_CTRUNC) {
1bdc94
+        error_report("Truncated message.");
1bdc94
+        goto err;
1bdc94
+    }
1bdc94
+
1bdc94
+    for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
1bdc94
+         cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
1bdc94
+            if (cmsg->cmsg_level == SOL_SOCKET &&
1bdc94
+                cmsg->cmsg_type == SCM_RIGHTS) {
1bdc94
+                    fdsize = cmsg->cmsg_len - CMSG_LEN(0);
1bdc94
+                    memcpy(&fd, CMSG_DATA(cmsg), fdsize);
1bdc94
+                    break;
1bdc94
+            }
1bdc94
+    }
1bdc94
+
1bdc94
     if (hdr.size > VHOST_USER_PAYLOAD_SIZE) {
1bdc94
         error_report("Failed to read msg header."
1bdc94
                 " Size %d exceeds the maximum %zu.", hdr.size,
1bdc94
@@ -883,9 +913,15 @@ static void slave_read(void *opaque)
1bdc94
         break;
1bdc94
     default:
1bdc94
         error_report("Received unexpected msg type.");
1bdc94
+        if (fd != -1) {
1bdc94
+            close(fd);
1bdc94
+        }
1bdc94
         ret = -EINVAL;
1bdc94
     }
1bdc94
 
1bdc94
+    /* Message handlers need to make sure that fd will be consumed. */
1bdc94
+    fd = -1;
1bdc94
+
1bdc94
     /*
1bdc94
      * REPLY_ACK feature handling. Other reply types has to be managed
1bdc94
      * directly in their request handlers.
1bdc94
@@ -918,6 +954,9 @@ err:
1bdc94
     qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
1bdc94
     close(u->slave_fd);
1bdc94
     u->slave_fd = -1;
1bdc94
+    if (fd != -1) {
1bdc94
+        close(fd);
1bdc94
+    }
1bdc94
     return;
1bdc94
 }
1bdc94
 
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94