All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: "Houqi (Nick) Zuo" <hzuo@redhat.com>
Cc: qemu-devel@nongnu.org, Jason Wang <jasowang@redhat.com>,
	Cindy Lu <lulu@redhat.com>, Michael Tsirkin <mst@redhat.com>
Subject: Re: [PATCH] net/net.c: add tap device fd validity check to prevent abort on deleted device
Date: Wed, 24 Sep 2025 08:04:30 +0100	[thread overview]
Message-ID: <aNOX_q7xoJeUY14e@redhat.com> (raw)
In-Reply-To: <20250924062831.1788305-1-hzuo@redhat.com>

On Wed, Sep 24, 2025 at 02:28:31PM +0800, Houqi (Nick) Zuo wrote:
> This patch addresses a scenario where QEMU would abort with a core dump
> when a tap device created by QEMU is manually deleted from the host while
> the guest is running.
> 
> The specific negative test case is:
> 1. Start QEMU with a tap device (created by QEMU)
> 2. Manually delete the tap device on the host
> 3. Execute shutdown in the guest
> 4. QEMU attempts to clean up the tap device but finds the file descriptor
>    in a bad state, leading to abort and core dump
> 
> The patch introduces a tap device file descriptor validity check using
> the TUNGETIFF ioctl to detect when the underlying tap device has been
> removed. When detected, the operations are skipped gracefully instead
> of proceeding with invalid file descriptors that cause ioctl failures.
> 
> The validity check is integrated into:
> - qemu_set_vnet_hdr_len() in net/net.c
> - qemu_set_offload() in net/net.c
> 
> This ensures that when the tap device is no longer valid, these functions
> return early without attempting operations that would fail and trigger
> aborts, thus achieving the expected behavior of error reporting without

> diff --git a/net/net.c b/net/net.c
> index da275db86e..c0750fd0b9 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -57,6 +57,7 @@
>  #include "qapi/string-output-visitor.h"
>  #include "qapi/qobject-input-visitor.h"
>  #include "standard-headers/linux/virtio_net.h"
> +#include "qemu/log.h"
>  
>  /* Net bridge is currently not supported for W32. */
>  #if !defined(_WIN32)
> @@ -543,7 +544,8 @@ bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
>  void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
>                            int ecn, int ufo, int uso4, int uso6)
>  {
> -    if (!nc || !nc->info->set_offload) {
> +    if (!nc || !nc->info->set_offload ||
> +        (nc->info->query_validity && nc->info->query_validity(nc) != 1)) {
>          return;
>      }
>  
> @@ -561,7 +563,8 @@ int qemu_get_vnet_hdr_len(NetClientState *nc)
>  
>  void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
>  {
> -    if (!nc || !nc->info->set_vnet_hdr_len) {
> +    if (!nc || !nc->info->set_vnet_hdr_len ||
> +        (nc->info->query_validity && nc->info->query_validity(nc) != 1)) {
>          return;
>      }
>

These checks leave open a race condition where the tap can be
deleted in between checking the tap and trying the operation.
Drop the check and just make the later operation not abort
on failure.

There are other methods in this file which will abort on
failure too tap that need addressing, because if setting
len failure is ignored it'll go on to call qemu_set_vnet_be
/ qemu_set_vnet_le eventually which will abort for the same
reason.


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  parent reply	other threads:[~2025-09-24  7:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-24  6:28 [PATCH] net/net.c: add tap device fd validity check to prevent abort on deleted device Houqi (Nick) Zuo
2025-09-24  6:32 ` Jason Wang
2025-09-24  6:54 ` Cindy Lu
2025-09-24  7:04 ` Daniel P. Berrangé [this message]
2025-09-24  8:14 ` [PATCH v2] net/tap-linux.c: avoid abort when setting invalid fd Houqi (Nick) Zuo
2025-09-24  8:26   ` Daniel P. Berrangé
2025-09-24  8:50 ` [PATCH v3] " Houqi (Nick) Zuo
2025-09-25  1:01   ` Cindy Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aNOX_q7xoJeUY14e@redhat.com \
    --to=berrange@redhat.com \
    --cc=hzuo@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=lulu@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.