yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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