qemu-e2k/net
Daniel P. Berrange e7b347d0bf net: detect errors from probing vnet hdr flag for TAP devices
When QEMU sets up a tap based network device backend, it mostly ignores errors
reported from various ioctl() calls it makes, assuming the TAP file descriptor
is valid. This assumption can easily be violated when the user is passing in a
pre-opened file descriptor. At best, the ioctls may fail with a -EBADF, but if
the user passes in a bogus FD number that happens to clash with a FD number that
QEMU has opened internally for another reason, a wide variety of errnos may
result, as the TUNGETIFF ioctl number may map to a completely different command
on a different type of file.

By ignoring all these errors, QEMU sets up a zombie network backend that will
never pass any data. Even worse, when QEMU shuts down, or that network backend
is hot-removed, it will close this bogus file descriptor, which could belong to
another QEMU device backend.

There's no obvious guaranteed reliable way to detect that a FD genuinely is a
TAP device, as opposed to a UNIX socket, or pipe, or something else. Checking
the errno from probing vnet hdr flag though, does catch the big common cases.
ie calling TUNGETIFF will return EBADF for an invalid FD, and ENOTTY when FD is
a UNIX socket, or pipe which catches accidental collisions with FDs used for
stdio, or monitor socket.

Previously the example below where bogus fd 9 collides with the FD used for the
chardev saw:

$ ./x86_64-softmmu/qemu-system-x86_64 -netdev tap,id=hostnet0,fd=9 \
  -chardev socket,id=charchannel0,path=/tmp/qga,server,nowait \
  -monitor stdio -vnc :0
qemu-system-x86_64: -netdev tap,id=hostnet0,fd=9: TUNGETIFF ioctl() failed: Inappropriate ioctl for device
TUNSETOFFLOAD ioctl() failed: Bad address
QEMU 2.9.1 monitor - type 'help' for more information
(qemu) Warning: netdev hostnet0 has no peer

which gives a running QEMU with a zombie network backend.

With this change applied we get an error message and QEMU immediately exits
before carrying on and making a bigger disaster:

$ ./x86_64-softmmu/qemu-system-x86_64 -netdev tap,id=hostnet0,fd=9 \
  -chardev socket,id=charchannel0,path=/tmp/qga,server,nowait \
  -monitor stdio -vnc :0
qemu-system-x86_64: -netdev tap,id=hostnet0,vhost=on,fd=9: Unable to query TUNGETIFF on FD 9: Inappropriate ioctl for device

Reported-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Tested-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-id: 20171027085548.3472-1-berrange@redhat.com
[lv: to simplify, don't check on EINVAL with TUNGETIFF as it exists since v2.6.27]
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2020-07-15 21:00:13 +08:00
..
can qom: Drop parameter @errp of object_property_add() & friends 2020-05-15 07:07:58 +02:00
Makefile.objs vhost-vdpa: introduce vhost-vdpa net client 2020-07-07 07:59:51 -04:00
announce.c net/announce: Add optional ID 2019-07-02 10:21:06 +08:00
checksum.c Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
clients.h vhost-vdpa: introduce vhost-vdpa net client 2020-07-07 07:59:51 -04:00
colo-compare.c net/colo-compare.c: Expose compare "max_queue_size" to users 2020-07-15 21:00:13 +08:00
colo-compare.h colo-compare: use notifier to notify packets comparing result 2018-10-19 11:15:03 +08:00
colo.c net/colo-compare.c: Fix deadlock in compare_chr_send 2020-06-18 21:05:52 +08:00
colo.h net/colo-compare.c: Fix deadlock in compare_chr_send 2020-06-18 21:05:52 +08:00
dump.c error: Eliminate error_propagate() with Coccinelle, part 1 2020-07-10 15:18:08 +02:00
eth.c Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
filter-buffer.c error: Eliminate error_propagate() with Coccinelle, part 1 2020-07-10 15:18:08 +02:00
filter-mirror.c qom: Drop parameter @errp of object_property_add() & friends 2020-05-15 07:07:58 +02:00
filter-replay.c Include qemu/module.h where needed, drop it from qemu-common.h 2019-06-12 13:18:33 +02:00
filter-rewriter.c qom: Drop parameter @errp of object_property_add() & friends 2020-05-15 07:07:58 +02:00
filter.c qom: Put name parameter before value / visitor parameter 2020-07-10 15:18:08 +02:00
hub.c hw/net: Make NetCanReceive() return a boolean 2020-03-31 21:14:35 +08:00
hub.h net: Remove deprecated [hub_id name] tuple of 'hostfwd_add' / 'hostfwd_remove' 2020-03-09 18:44:04 +00:00
l2tpv3.c Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
net.c qom: Put name parameter before value / visitor parameter 2020-07-10 15:18:08 +02:00
netmap.c Include qemu/main-loop.h less 2019-08-16 13:31:52 +02:00
queue.c misc: Replace zero-length arrays with flexible array member (automatic) 2020-03-16 22:07:42 +01:00
slirp.c net: Remove deprecated [hub_id name] tuple of 'hostfwd_add' / 'hostfwd_remove' 2020-03-09 18:44:04 +00:00
socket.c net: check if the file descriptor is valid before using it 2020-07-15 21:00:13 +08:00
tap-bsd.c net: detect errors from probing vnet hdr flag for TAP devices 2020-07-15 21:00:13 +08:00
tap-linux.c net: detect errors from probing vnet hdr flag for TAP devices 2020-07-15 21:00:13 +08:00
tap-linux.h Clean up decorations and whitespace around header guards 2016-07-12 16:20:46 +02:00
tap-solaris.c net: detect errors from probing vnet hdr flag for TAP devices 2020-07-15 21:00:13 +08:00
tap-stub.c net: detect errors from probing vnet hdr flag for TAP devices 2020-07-15 21:00:13 +08:00
tap-win32.c Clean up inclusion of sysemu/sysemu.h 2019-08-16 13:31:53 +02:00
tap.c net: detect errors from probing vnet hdr flag for TAP devices 2020-07-15 21:00:13 +08:00
tap_int.h net: detect errors from probing vnet hdr flag for TAP devices 2020-07-15 21:00:13 +08:00
trace-events colo-compare: Fix memory leak in packet_enqueue() 2020-06-18 21:05:52 +08:00
util.c net: Clean up includes 2016-02-04 17:41:30 +00:00
util.h net: do not depend on slirp internals 2019-01-14 00:44:29 +01:00
vde.c net/vde: print error on vde_open() failure 2018-03-26 14:52:43 +08:00
vhost-user-stub.c vhost-net-user: add stubs for when no virtio-net device is present 2019-02-21 12:28:01 -05:00
vhost-user.c chardev: Use QEMUChrEvent enum in IOEventHandler typedef 2020-01-08 11:15:35 +01:00
vhost-vdpa.c vhost-vdpa: introduce vhost-vdpa net client 2020-07-07 07:59:51 -04:00