yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
6e7d01
From 3475ea6598896edb689ca8ba6fb81781e2517b6f Mon Sep 17 00:00:00 2001
6e7d01
From: Laurent Vivier <lvivier@redhat.com>
6e7d01
Date: Thu, 29 Jul 2021 04:56:49 -0400
6e7d01
Subject: [PATCH 14/14] net: detect errors from probing vnet hdr flag for TAP
6e7d01
 devices
6e7d01
MIME-Version: 1.0
6e7d01
Content-Type: text/plain; charset=UTF-8
6e7d01
Content-Transfer-Encoding: 8bit
6e7d01
6e7d01
RH-Author: Laurent Vivier <lvivier@redhat.com>
6e7d01
Message-id: <20210726102337.6359-3-lvivier@redhat.com>
6e7d01
Patchwork-id: 101923
6e7d01
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 2/2] net: detect errors from probing vnet hdr flag for TAP devices
6e7d01
Bugzilla: 1982134
6e7d01
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
6e7d01
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
6e7d01
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
6e7d01
6e7d01
From: "Daniel P. Berrange" <berrange@redhat.com>
6e7d01
6e7d01
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1982134
6e7d01
BRANCH: rhel-8.5.0
6e7d01
UPSTREAM: Merged
6e7d01
BREW: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=38380653
6e7d01
6e7d01
When QEMU sets up a tap based network device backend, it mostly ignores errors
6e7d01
reported from various ioctl() calls it makes, assuming the TAP file descriptor
6e7d01
is valid. This assumption can easily be violated when the user is passing in a
6e7d01
pre-opened file descriptor. At best, the ioctls may fail with a -EBADF, but if
6e7d01
the user passes in a bogus FD number that happens to clash with a FD number that
6e7d01
QEMU has opened internally for another reason, a wide variety of errnos may
6e7d01
result, as the TUNGETIFF ioctl number may map to a completely different command
6e7d01
on a different type of file.
6e7d01
6e7d01
By ignoring all these errors, QEMU sets up a zombie network backend that will
6e7d01
never pass any data. Even worse, when QEMU shuts down, or that network backend
6e7d01
is hot-removed, it will close this bogus file descriptor, which could belong to
6e7d01
another QEMU device backend.
6e7d01
6e7d01
There's no obvious guaranteed reliable way to detect that a FD genuinely is a
6e7d01
TAP device, as opposed to a UNIX socket, or pipe, or something else. Checking
6e7d01
the errno from probing vnet hdr flag though, does catch the big common cases.
6e7d01
ie calling TUNGETIFF will return EBADF for an invalid FD, and ENOTTY when FD is
6e7d01
a UNIX socket, or pipe which catches accidental collisions with FDs used for
6e7d01
stdio, or monitor socket.
6e7d01
6e7d01
Previously the example below where bogus fd 9 collides with the FD used for the
6e7d01
chardev saw:
6e7d01
6e7d01
$ ./x86_64-softmmu/qemu-system-x86_64 -netdev tap,id=hostnet0,fd=9 \
6e7d01
  -chardev socket,id=charchannel0,path=/tmp/qga,server,nowait \
6e7d01
  -monitor stdio -vnc :0
6e7d01
qemu-system-x86_64: -netdev tap,id=hostnet0,fd=9: TUNGETIFF ioctl() failed: Inappropriate ioctl for device
6e7d01
TUNSETOFFLOAD ioctl() failed: Bad address
6e7d01
QEMU 2.9.1 monitor - type 'help' for more information
6e7d01
(qemu) Warning: netdev hostnet0 has no peer
6e7d01
6e7d01
which gives a running QEMU with a zombie network backend.
6e7d01
6e7d01
With this change applied we get an error message and QEMU immediately exits
6e7d01
before carrying on and making a bigger disaster:
6e7d01
6e7d01
$ ./x86_64-softmmu/qemu-system-x86_64 -netdev tap,id=hostnet0,fd=9 \
6e7d01
  -chardev socket,id=charchannel0,path=/tmp/qga,server,nowait \
6e7d01
  -monitor stdio -vnc :0
6e7d01
qemu-system-x86_64: -netdev tap,id=hostnet0,vhost=on,fd=9: Unable to query TUNGETIFF on FD 9: Inappropriate ioctl for device
6e7d01
6e7d01
Reported-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
6e7d01
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
6e7d01
Tested-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
6e7d01
Message-id: 20171027085548.3472-1-berrange@redhat.com
6e7d01
[lv: to simplify, don't check on EINVAL with TUNGETIFF as it exists since v2.6.27]
6e7d01
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
6e7d01
Signed-off-by: Jason Wang <jasowang@redhat.com>
6e7d01
(cherry picked from commit e7b347d0bf640adb1c998d317eaf44d2d7cbd973)
6e7d01
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
6e7d01
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
6e7d01
---
6e7d01
 net/tap-bsd.c     |  2 +-
6e7d01
 net/tap-linux.c   |  8 +++++---
6e7d01
 net/tap-solaris.c |  2 +-
6e7d01
 net/tap-stub.c    |  2 +-
6e7d01
 net/tap.c         | 25 ++++++++++++++++++++-----
6e7d01
 net/tap_int.h     |  2 +-
6e7d01
 6 files changed, 29 insertions(+), 12 deletions(-)
6e7d01
6e7d01
diff --git a/net/tap-bsd.c b/net/tap-bsd.c
6e7d01
index a5c3707f80..77aaf674b1 100644
6e7d01
--- a/net/tap-bsd.c
6e7d01
+++ b/net/tap-bsd.c
6e7d01
@@ -211,7 +211,7 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
6e7d01
 {
6e7d01
 }
6e7d01
 
6e7d01
-int tap_probe_vnet_hdr(int fd)
6e7d01
+int tap_probe_vnet_hdr(int fd, Error **errp)
6e7d01
 {
6e7d01
     return 0;
6e7d01
 }
6e7d01
diff --git a/net/tap-linux.c b/net/tap-linux.c
6e7d01
index e0dd442ee3..b0635e9e32 100644
6e7d01
--- a/net/tap-linux.c
6e7d01
+++ b/net/tap-linux.c
6e7d01
@@ -147,13 +147,15 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
6e7d01
     }
6e7d01
 }
6e7d01
 
6e7d01
-int tap_probe_vnet_hdr(int fd)
6e7d01
+int tap_probe_vnet_hdr(int fd, Error **errp)
6e7d01
 {
6e7d01
     struct ifreq ifr;
6e7d01
 
6e7d01
     if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
6e7d01
-        error_report("TUNGETIFF ioctl() failed: %s", strerror(errno));
6e7d01
-        return 0;
6e7d01
+        /* TUNGETIFF is available since kernel v2.6.27 */
6e7d01
+        error_setg_errno(errp, errno,
6e7d01
+                         "Unable to query TUNGETIFF on FD %d", fd);
6e7d01
+        return -1;
6e7d01
     }
6e7d01
 
6e7d01
     return ifr.ifr_flags & IFF_VNET_HDR;
6e7d01
diff --git a/net/tap-solaris.c b/net/tap-solaris.c
6e7d01
index 4725d2314e..ae2ba68284 100644
6e7d01
--- a/net/tap-solaris.c
6e7d01
+++ b/net/tap-solaris.c
6e7d01
@@ -206,7 +206,7 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
6e7d01
 {
6e7d01
 }
6e7d01
 
6e7d01
-int tap_probe_vnet_hdr(int fd)
6e7d01
+int tap_probe_vnet_hdr(int fd, Error **errp)
6e7d01
 {
6e7d01
     return 0;
6e7d01
 }
6e7d01
diff --git a/net/tap-stub.c b/net/tap-stub.c
6e7d01
index a9ab8f8293..de525a2e69 100644
6e7d01
--- a/net/tap-stub.c
6e7d01
+++ b/net/tap-stub.c
6e7d01
@@ -37,7 +37,7 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
6e7d01
 {
6e7d01
 }
6e7d01
 
6e7d01
-int tap_probe_vnet_hdr(int fd)
6e7d01
+int tap_probe_vnet_hdr(int fd, Error **errp)
6e7d01
 {
6e7d01
     return 0;
6e7d01
 }
6e7d01
diff --git a/net/tap.c b/net/tap.c
6e7d01
index 41a20102fd..b37ccae00c 100644
6e7d01
--- a/net/tap.c
6e7d01
+++ b/net/tap.c
6e7d01
@@ -597,7 +597,11 @@ int net_init_bridge(const Netdev *netdev, const char *name,
6e7d01
     }
6e7d01
 
6e7d01
     qemu_set_nonblock(fd);
6e7d01
-    vnet_hdr = tap_probe_vnet_hdr(fd);
6e7d01
+    vnet_hdr = tap_probe_vnet_hdr(fd, errp);
6e7d01
+    if (vnet_hdr < 0) {
6e7d01
+        close(fd);
6e7d01
+        return -1;
6e7d01
+    }
6e7d01
     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
6e7d01
 
6e7d01
     snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
6e7d01
@@ -810,7 +814,11 @@ int net_init_tap(const Netdev *netdev, const char *name,
6e7d01
             return -1;
6e7d01
         }
6e7d01
 
6e7d01
-        vnet_hdr = tap_probe_vnet_hdr(fd);
6e7d01
+        vnet_hdr = tap_probe_vnet_hdr(fd, errp);
6e7d01
+        if (vnet_hdr < 0) {
6e7d01
+            close(fd);
6e7d01
+            return -1;
6e7d01
+        }
6e7d01
 
6e7d01
         net_init_tap_one(tap, peer, "tap", name, NULL,
6e7d01
                          script, downscript,
6e7d01
@@ -863,8 +871,11 @@ int net_init_tap(const Netdev *netdev, const char *name,
6e7d01
             }
6e7d01
 
6e7d01
             if (i == 0) {
6e7d01
-                vnet_hdr = tap_probe_vnet_hdr(fd);
6e7d01
-            } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
6e7d01
+                vnet_hdr = tap_probe_vnet_hdr(fd, errp);
6e7d01
+                if (vnet_hdr < 0) {
6e7d01
+                    goto free_fail;
6e7d01
+                }
6e7d01
+            } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) {
6e7d01
                 error_setg(errp,
6e7d01
                            "vnet_hdr not consistent across given tap fds");
6e7d01
                 ret = -1;
6e7d01
@@ -909,7 +920,11 @@ free_fail:
6e7d01
         }
6e7d01
 
6e7d01
         qemu_set_nonblock(fd);
6e7d01
-        vnet_hdr = tap_probe_vnet_hdr(fd);
6e7d01
+        vnet_hdr = tap_probe_vnet_hdr(fd, errp);
6e7d01
+        if (vnet_hdr < 0) {
6e7d01
+            close(fd);
6e7d01
+            return -1;
6e7d01
+        }
6e7d01
 
6e7d01
         net_init_tap_one(tap, peer, "bridge", name, ifname,
6e7d01
                          script, downscript, vhostfdname,
6e7d01
diff --git a/net/tap_int.h b/net/tap_int.h
6e7d01
index e3194b23f4..225a49ea48 100644
6e7d01
--- a/net/tap_int.h
6e7d01
+++ b/net/tap_int.h
6e7d01
@@ -34,7 +34,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
6e7d01
 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen);
6e7d01
 
6e7d01
 void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp);
6e7d01
-int tap_probe_vnet_hdr(int fd);
6e7d01
+int tap_probe_vnet_hdr(int fd, Error **errp);
6e7d01
 int tap_probe_vnet_hdr_len(int fd, int len);
6e7d01
 int tap_probe_has_ufo(int fd);
6e7d01
 void tap_fd_set_offload(int fd, int csum, int tso4, int tso6, int ecn, int ufo);
6e7d01
-- 
6e7d01
2.27.0
6e7d01