linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Jason Wang <jasowang@redhat.com>
Cc: virtualization <virtualization@lists.linux-foundation.org>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	"Hetzelt, Felicitas" <f.hetzelt@tu-berlin.de>,
	"kaplan, david" <david.kaplan@amd.com>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	pbonzini <pbonzini@redhat.com>, Andi Kleen <ak@linux.intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	"Kuppuswamy,
	Sathyanarayanan"  <sathyanarayanan.kuppuswamy@linux.intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Andy Lutomirski <luto@kernel.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Richard Henderson <rth@twiddle.net>,
	Thomas Bogendoerfer <tsbogend@alpha.franken.de>,
	James E J Bottomley <James.Bottomley@hansenpartnership.com>,
	Helge Deller <deller@gmx.de>,
	"David S . Miller" <davem@davemloft.net>,
	Arnd Bergmann <arnd@arndb.de>, Jonathan Corbet <corbet@lwn.net>,
	Peter H Anvin <hpa@zytor.com>,
	Dave Hansen <dave.hansen@intel.com>,
	Tony Luck <tony.luck@intel.com>,
	Kirill Shutemov <kirill.shutemov@linux.intel.com>,
	Sean Christopherson <seanjc@google.com>,
	Kuppuswamy Sathyanarayanan <knsathya@kernel.org>,
	X86 ML <x86@kernel.org>
Subject: Re: [PATCH 6/9] virtio_pci: harden MSI-X interrupts
Date: Mon, 13 Sep 2021 03:01:07 -0400	[thread overview]
Message-ID: <20210913025729-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <CACGkMEuP=uHXrpeUa4Fku3ZoHm12y2R4_htA=_0nr-0HYBiB0A@mail.gmail.com>

On Mon, Sep 13, 2021 at 02:43:08PM +0800, Jason Wang wrote:
> On Mon, Sep 13, 2021 at 2:37 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > On Mon, Sep 13, 2021 at 02:34:01PM +0800, Jason Wang wrote:
> > > On Mon, Sep 13, 2021 at 2:28 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> > > >
> > > > On Mon, Sep 13, 2021 at 02:08:02PM +0800, Jason Wang wrote:
> > > > > On Mon, Sep 13, 2021 at 2:04 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> > > > > >
> > > > > > On Mon, Sep 13, 2021 at 01:53:50PM +0800, Jason Wang wrote:
> > > > > > > We used to synchronize pending MSI-X irq handlers via
> > > > > > > synchronize_irq(), this may not work for the untrusted device which
> > > > > > > may keep sending interrupts after reset which may lead unexpected
> > > > > > > results. Similarly, we should not enable MSI-X interrupt until the
> > > > > > > device is ready. So this patch fixes those two issues by:
> > > > > > >
> > > > > > > 1) switching to use disable_irq() to prevent the virtio interrupt
> > > > > > >    handlers to be called after the device is reset.
> > > > > > > 2) using IRQF_NO_AUTOEN and enable the MSI-X irq during .ready()
> > > > > > >
> > > > > > > This can make sure the virtio interrupt handler won't be called before
> > > > > > > virtio_device_ready() and after reset.
> > > > > > >
> > > > > > > Signed-off-by: Jason Wang <jasowang@redhat.com>
> > > > > >
> > > > > > I don't get the threat model here. Isn't disabling irqs done by the
> > > > > > hypervisor anyway? Is there a reason to trust disable_irq but not
> > > > > > device reset?
> > > > >
> > > > > My understanding is that e.g in the case of SEV/TDX we don't trust the
> > > > > hypervisor. So the hypervisor can keep sending interrupts even if the
> > > > > device is reset. The guest can only trust its own software interrupt
> > > > > management logic to avoid call virtio callback in this case.
> > > > >
> > > > > Thanks
> > > >
> > > > Hmm but I don't see how do these patches do this.
> > > > They call disable_irq but can't the hypervisor keep
> > > > sending interrupts after disable_irq, too?
> > >
> > > Yes, but since the irq is disabled, the vring or config callback won't
> > > be called in this case.
> > >
> > > Thanks
> >
> > But doen't "irq is disabled" basically mean "we told the hypervisor
> > to disable the irq"?  What extractly prevents hypervisor from
> > sending the irq even if guest thinks it disabled it?
> 
> It can't prevent the hypersior from sending irq. But it can make sure
> the irq descriptor is disabled (e.g IRQD_IRQ_DISABLED). Is this
> sufficient?
> 
> Thanks

Maybe, maybe not ... there's not a lot in the way of
memory barriers around code using that bit, that's for sure.
Did anyone look at it from point of view of what
can a bad interrupt do?


> >
> > > >
> > > >
> > > >
> > > > > >
> > > > > > Cc a bunch more people ...
> > > > > >
> > > > > >
> > > > > > > ---
> > > > > > >  drivers/virtio/virtio_pci_common.c | 27 +++++++++++++++++++++------
> > > > > > >  drivers/virtio/virtio_pci_common.h |  6 ++++--
> > > > > > >  drivers/virtio/virtio_pci_legacy.c |  5 +++--
> > > > > > >  drivers/virtio/virtio_pci_modern.c |  6 ++++--
> > > > > > >  4 files changed, 32 insertions(+), 12 deletions(-)
> > > > > > >
> > > > > > > diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> > > > > > > index b35bb2d57f62..0b9523e6dd39 100644
> > > > > > > --- a/drivers/virtio/virtio_pci_common.c
> > > > > > > +++ b/drivers/virtio/virtio_pci_common.c
> > > > > > > @@ -24,8 +24,8 @@ MODULE_PARM_DESC(force_legacy,
> > > > > > >                "Force legacy mode for transitional virtio 1 devices");
> > > > > > >  #endif
> > > > > > >
> > > > > > > -/* wait for pending irq handlers */
> > > > > > > -void vp_synchronize_vectors(struct virtio_device *vdev)
> > > > > > > +/* disable irq handlers */
> > > > > > > +void vp_disable_vectors(struct virtio_device *vdev)
> > > > > > >  {
> > > > > > >       struct virtio_pci_device *vp_dev = to_vp_device(vdev);
> > > > > > >       int i;
> > > > > > > @@ -34,7 +34,20 @@ void vp_synchronize_vectors(struct virtio_device *vdev)
> > > > > > >               synchronize_irq(vp_dev->pci_dev->irq);
> > > > > > >
> > > > > > >       for (i = 0; i < vp_dev->msix_vectors; ++i)
> > > > > > > -             synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
> > > > > > > +             disable_irq(pci_irq_vector(vp_dev->pci_dev, i));
> > > > > > > +}
> > > > > > > +
> > > > > > > +/* enable irq handlers */
> > > > > > > +void vp_enable_vectors(struct virtio_device *vdev)
> > > > > > > +{
> > > > > > > +     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
> > > > > > > +     int i;
> > > > > > > +
> > > > > > > +     if (vp_dev->intx_enabled)
> > > > > > > +             return;
> > > > > > > +
> > > > > > > +     for (i = 0; i < vp_dev->msix_vectors; ++i)
> > > > > > > +             enable_irq(pci_irq_vector(vp_dev->pci_dev, i));
> > > > > > >  }
> > > > > > >
> > > > > > >  /* the notify function used when creating a virt queue */
> > > > > > > @@ -141,7 +154,8 @@ static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
> > > > > > >       snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
> > > > > > >                "%s-config", name);
> > > > > > >       err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
> > > > > > > -                       vp_config_changed, 0, vp_dev->msix_names[v],
> > > > > > > +                       vp_config_changed, IRQF_NO_AUTOEN,
> > > > > > > +                       vp_dev->msix_names[v],
> > > > > > >                         vp_dev);
> > > > > > >       if (err)
> > > > > > >               goto error;
> > > > > > > @@ -160,7 +174,8 @@ static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
> > > > > > >               snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
> > > > > > >                        "%s-virtqueues", name);
> > > > > > >               err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
> > > > > > > -                               vp_vring_interrupt, 0, vp_dev->msix_names[v],
> > > > > > > +                               vp_vring_interrupt, IRQF_NO_AUTOEN,
> > > > > > > +                               vp_dev->msix_names[v],
> > > > > > >                                 vp_dev);
> > > > > > >               if (err)
> > > > > > >                       goto error;
> > > > > > > @@ -337,7 +352,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
> > > > > > >                        "%s-%s",
> > > > > > >                        dev_name(&vp_dev->vdev.dev), names[i]);
> > > > > > >               err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
> > > > > > > -                               vring_interrupt, 0,
> > > > > > > +                               vring_interrupt, IRQF_NO_AUTOEN,
> > > > > > >                                 vp_dev->msix_names[msix_vec],
> > > > > > >                                 vqs[i]);
> > > > > > >               if (err)
> > > > > > > diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h
> > > > > > > index beec047a8f8d..a235ce9ff6a5 100644
> > > > > > > --- a/drivers/virtio/virtio_pci_common.h
> > > > > > > +++ b/drivers/virtio/virtio_pci_common.h
> > > > > > > @@ -102,8 +102,10 @@ static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
> > > > > > >       return container_of(vdev, struct virtio_pci_device, vdev);
> > > > > > >  }
> > > > > > >
> > > > > > > -/* wait for pending irq handlers */
> > > > > > > -void vp_synchronize_vectors(struct virtio_device *vdev);
> > > > > > > +/* disable irq handlers */
> > > > > > > +void vp_disable_vectors(struct virtio_device *vdev);
> > > > > > > +/* enable irq handlers */
> > > > > > > +void vp_enable_vectors(struct virtio_device *vdev);
> > > > > > >  /* the notify function used when creating a virt queue */
> > > > > > >  bool vp_notify(struct virtqueue *vq);
> > > > > > >  /* the config->del_vqs() implementation */
> > > > > > > diff --git a/drivers/virtio/virtio_pci_legacy.c b/drivers/virtio/virtio_pci_legacy.c
> > > > > > > index d62e9835aeec..bdf6bc667ab5 100644
> > > > > > > --- a/drivers/virtio/virtio_pci_legacy.c
> > > > > > > +++ b/drivers/virtio/virtio_pci_legacy.c
> > > > > > > @@ -97,8 +97,8 @@ static void vp_reset(struct virtio_device *vdev)
> > > > > > >       /* Flush out the status write, and flush in device writes,
> > > > > > >        * including MSi-X interrupts, if any. */
> > > > > > >       ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
> > > > > > > -     /* Flush pending VQ/configuration callbacks. */
> > > > > > > -     vp_synchronize_vectors(vdev);
> > > > > > > +     /* Disable VQ/configuration callbacks. */
> > > > > > > +     vp_disable_vectors(vdev);
> > > > > > >  }
> > > > > > >
> > > > > > >  static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
> > > > > > > @@ -194,6 +194,7 @@ static void del_vq(struct virtio_pci_vq_info *info)
> > > > > > >  }
> > > > > > >
> > > > > > >  static const struct virtio_config_ops virtio_pci_config_ops = {
> > > > > > > +     .ready          = vp_enable_vectors,
> > > > > > >       .get            = vp_get,
> > > > > > >       .set            = vp_set,
> > > > > > >       .get_status     = vp_get_status,
> > > > > > > diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
> > > > > > > index 30654d3a0b41..acf0f6b6381d 100644
> > > > > > > --- a/drivers/virtio/virtio_pci_modern.c
> > > > > > > +++ b/drivers/virtio/virtio_pci_modern.c
> > > > > > > @@ -172,8 +172,8 @@ static void vp_reset(struct virtio_device *vdev)
> > > > > > >        */
> > > > > > >       while (vp_modern_get_status(mdev))
> > > > > > >               msleep(1);
> > > > > > > -     /* Flush pending VQ/configuration callbacks. */
> > > > > > > -     vp_synchronize_vectors(vdev);
> > > > > > > +     /* Disable VQ/configuration callbacks. */
> > > > > > > +     vp_disable_vectors(vdev);
> > > > > > >  }
> > > > > > >
> > > > > > >  static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
> > > > > > > @@ -380,6 +380,7 @@ static bool vp_get_shm_region(struct virtio_device *vdev,
> > > > > > >  }
> > > > > > >
> > > > > > >  static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
> > > > > > > +     .ready          = vp_enable_vectors,
> > > > > > >       .get            = NULL,
> > > > > > >       .set            = NULL,
> > > > > > >       .generation     = vp_generation,
> > > > > > > @@ -397,6 +398,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
> > > > > > >  };
> > > > > > >
> > > > > > >  static const struct virtio_config_ops virtio_pci_config_ops = {
> > > > > > > +     .ready          = vp_enable_vectors,
> > > > > > >       .get            = vp_get,
> > > > > > >       .set            = vp_set,
> > > > > > >       .generation     = vp_generation,
> > > > > > > --
> > > > > > > 2.25.1
> > > > > >
> > > >
> >


  reply	other threads:[~2021-09-13  7:01 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-13  5:53 [PATCH 0/9] More virtio hardening Jason Wang
2021-09-13  5:53 ` [PATCH 1/9] virtio-blk: validate num_queues during probe Jason Wang
2021-09-13  7:48   ` Stefano Garzarella
2021-09-14  2:29     ` Jason Wang
2021-09-13 12:05   ` Stefan Hajnoczi
2021-09-13  5:53 ` [PATCH 2/9] virtio: add doc for validate() method Jason Wang
2021-09-13  5:53 ` [PATCH 3/9] virtio-console: switch to use .validate() Jason Wang
2021-09-13  5:53 ` [PATCH 4/9] virtio_console: validate max_nr_ports before trying to use it Jason Wang
2021-09-13  5:53 ` [PATCH 5/9] virtio_config: introduce a new ready method Jason Wang
2021-09-13  5:53 ` [PATCH 6/9] virtio_pci: harden MSI-X interrupts Jason Wang
2021-09-13  6:03   ` Michael S. Tsirkin
2021-09-13  6:08     ` Jason Wang
2021-09-13  6:28       ` Michael S. Tsirkin
2021-09-13  6:34         ` Jason Wang
2021-09-13  6:37           ` Michael S. Tsirkin
2021-09-13  6:43             ` Jason Wang
2021-09-13  7:01               ` Michael S. Tsirkin [this message]
2021-09-13  7:15                 ` Jason Wang
2021-09-13  6:50             ` Michael S. Tsirkin
2021-09-13  7:07               ` Jason Wang
2021-09-13 19:38                 ` Thomas Gleixner
2021-09-13 20:54                   ` Michael S. Tsirkin
2021-09-13 22:31                     ` Thomas Gleixner
2021-09-14  2:20                       ` Jason Wang
2021-09-14  8:29                     ` Thomas Gleixner
2021-09-13  5:53 ` [PATCH 7/9] virtio-pci: harden INTX interrupts Jason Wang
2021-09-13  6:33   ` Michael S. Tsirkin
2021-09-13  6:36     ` Jason Wang
2021-09-13  6:41       ` Michael S. Tsirkin
2021-09-13  6:45         ` Jason Wang
2021-09-13  7:02           ` Michael S. Tsirkin
2021-09-13  7:17             ` Jason Wang
2021-09-13 21:36   ` Thomas Gleixner
2021-09-13 22:01     ` Michael S. Tsirkin
2021-09-13 22:20       ` Thomas Gleixner
2021-09-14  2:50     ` Jason Wang
2021-09-14  9:34     ` Boqun Feng
2021-09-14 11:03     ` Peter Zijlstra
2021-09-14 11:09       ` Thomas Gleixner
2021-09-13  5:53 ` [PATCH 8/9] virtio_ring: fix typos in vring_desc_extra Jason Wang
2021-09-13  5:53 ` [PATCH 9/9] virtio_ring: validate used buffer length Jason Wang
2021-09-13  6:36   ` Michael S. Tsirkin
2021-09-13  6:40     ` Jason Wang
2021-09-13  6:57       ` Michael S. Tsirkin
2021-09-13  7:13         ` Jason Wang
2021-10-05  7:42 ` [PATCH 0/9] More virtio hardening Michael S. Tsirkin
2021-10-11  7:36   ` Jason Wang
2021-10-11 12:36     ` Michael S. Tsirkin
2021-10-12  2:43       ` Jason Wang
2021-10-12  5:44         ` Michael S. Tsirkin
2021-10-12  6:11           ` Jason Wang
2021-10-12  6:35             ` Michael S. Tsirkin
2021-10-12  6:43               ` Jason Wang
2021-10-12  7:03                 ` Michael S. Tsirkin
2021-10-12  8:46                   ` Jason Wang

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=20210913025729-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=ak@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=bhelgaas@google.com \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=davem@davemloft.net \
    --cc=david.kaplan@amd.com \
    --cc=deller@gmx.de \
    --cc=f.hetzelt@tu-berlin.de \
    --cc=hpa@zytor.com \
    --cc=jasowang@redhat.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=knsathya@kernel.org \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rth@twiddle.net \
    --cc=sathyanarayanan.kuppuswamy@linux.intel.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=tsbogend@alpha.franken.de \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=x86@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).