yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-net-detect-errors-from-probing-vnet-hdr-flag-for-TAP.patch

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