All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Scull <ascull@google.com>
To: Bin Meng <bmeng.cn@gmail.com>
Cc: "U-Boot Mailing List" <u-boot@lists.denx.de>,
	"Simon Glass" <sjg@chromium.org>,
	"Alistair Delva" <adelva@google.com>,
	"Keir Fraser" <keirf@google.com>,
	"Pierre-Clément Tosi" <ptosi@google.com>
Subject: Re: [PATCH 07/11] virtio: pci: Check virtio configs are mapped
Date: Fri, 25 Mar 2022 07:07:43 +0000	[thread overview]
Message-ID: <CADcWuH0PmahWAsPd7MyAJHechjxXii=WjtvWmL3WaMn_enBk-Q@mail.gmail.com> (raw)
In-Reply-To: <CAEUhbmX4yvWUnWx2+Z+Seh3guZrn79SqrK=+=FBOpZaG8c7P8w@mail.gmail.com>

On Fri, 25 Mar 2022 at 04:38, Bin Meng <bmeng.cn@gmail.com> wrote:
>
> On Sun, Mar 20, 2022 at 7:42 PM Andrew Scull <ascull@google.com> wrote:
> >
> > The calls to `virtio_pci_map_capability()` return NULL on error. If this
> > happens, later accesses to the pointers would be unsafe. Avoid this by
> > failing if the configs were not successfully mapped.
> >
> > Signed-off-by: Andrew Scull <ascull@google.com>
> > ---
> >  drivers/virtio/virtio_pci_modern.c | 25 ++++++++++++++++++++-----
> >  1 file changed, 20 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
> > index 38a0da0a84..2f1a1cedbc 100644
> > --- a/drivers/virtio/virtio_pci_modern.c
> > +++ b/drivers/virtio/virtio_pci_modern.c
> > @@ -534,7 +534,19 @@ static int virtio_pci_probe(struct udevice *udev)
> >                 return -EINVAL;
> >         }
> >
> > +       /* Map configuration structures */
> > +       priv->common = virtio_pci_map_capability(udev, &common_cap);
> > +       if (!priv->common) {
>
> This can't be NULL, as you did not pass a NULL capability pointer
>
> > +               printf("(%s): could not map common config\n", udev->name);
> > +               return -EINVAL;
> > +       }
> > +
> >         priv->notify_len = notify_cap.length;
> > +       priv->notify_base = virtio_pci_map_capability(udev, &notify_cap);
> > +       if (!priv->notify_base) {
> > +               printf("(%s): could not map notify config\n", udev->name);
> > +               return -EINVAL;
> > +       }
> >
> >         /*
> >          * Device capability is only mandatory for devices that have
> > @@ -543,13 +555,16 @@ static int virtio_pci_probe(struct udevice *udev)
> >         device = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_DEVICE_CFG,
> >                                             sizeof(struct virtio_pci_cap),
> >                                             &device_cap);
> > -       if (device)
> > +       if (device) {
> >                 priv->device_len = device_cap.length;
> > +               priv->device = virtio_pci_map_capability(udev, &device_cap);
> > +               if (!priv->device) {
> > +                       printf("(%s): could not map device config\n",
> > +                              udev->name);
> > +                       return -EINVAL;
> > +               }
> > +       }
> >
> > -       /* Map configuration structures */
> > -       priv->common = virtio_pci_map_capability(udev, &common_cap);
> > -       priv->notify_base = virtio_pci_map_capability(udev, &notify_cap);
> > -       priv->device = virtio_pci_map_capability(udev, &device_cap);
> >         debug("(%p): common @ %p, notify base @ %p, device @ %p\n",
> >               udev, priv->common, priv->notify_base, priv->device);
> >
>
> I don't think adding these checks is necessary.

See later patches in the series that validate the address range is
within the declared PCI ranges and not an arbitrary range chosen,
accidently or maliciously, by the device. If those checks fail, the
memory will not have been mapped and the probe should fail.

> Regards,
> Bin

  reply	other threads:[~2022-03-25  7:08 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-20 11:41 [PATCH 00/11] virtio: pci: Add and fix consistency checks Andrew Scull
2022-03-20 11:41 ` [PATCH 01/11] virtio: pci: Fix discovery of device config length Andrew Scull
2022-03-24  9:32   ` Bin Meng
2022-03-20 11:41 ` [PATCH 02/11] virtio: pci: Bounds check device config access Andrew Scull
2022-03-24 14:07   ` Bin Meng
2022-03-20 11:41 ` [PATCH 03/11] virtio: pci: Bounds check notification writes Andrew Scull
2022-03-24 14:18   ` Bin Meng
2022-03-24 16:24     ` Andrew Scull
2022-03-25  1:41       ` Bin Meng
2022-03-20 11:41 ` [PATCH 04/11] virtio: pci: Check virtio common config size Andrew Scull
2022-03-24 14:22   ` Bin Meng
2022-03-20 11:41 ` [PATCH 05/11] virtio: pci: Check virtio capability is in bounds Andrew Scull
2022-03-24 15:24   ` Bin Meng
2022-03-24 16:27     ` Andrew Scull
2022-03-25  1:27       ` Bin Meng
2022-03-20 11:41 ` [PATCH 06/11] virtio: pci: Read entire capability into memory Andrew Scull
2022-03-25  4:31   ` Bin Meng
2022-03-25  7:03     ` Andrew Scull
2022-03-25  7:51       ` Bin Meng
2022-03-25  9:18         ` Andrew Scull
2022-03-25 10:25           ` Bin Meng
2022-03-28 14:28             ` Andrew Scull
2022-03-20 11:41 ` [PATCH 07/11] virtio: pci: Check virtio configs are mapped Andrew Scull
2022-03-25  4:38   ` Bin Meng
2022-03-25  7:07     ` Andrew Scull [this message]
2022-03-25  7:19       ` Bin Meng
2022-03-20 11:41 ` [PATCH 08/11] pci: Check region ranges are addressable Andrew Scull
2022-03-25  7:14   ` Bin Meng
2022-03-20 11:41 ` [PATCH 09/11] pci: Add function to validate PCI address range Andrew Scull
2022-03-25  7:14   ` Bin Meng
2022-03-25 10:26     ` Andrew Scull
2022-03-20 11:41 ` [PATCH 10/11] virtio: pci: Check mapped range is in a PCI region Andrew Scull
2022-03-25  7:14   ` Bin Meng
2022-03-20 11:41 ` [PATCH 11/11] virtio: pci: Allow exclusion of legacy driver Andrew Scull
2022-03-25  7:14   ` Bin Meng

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='CADcWuH0PmahWAsPd7MyAJHechjxXii=WjtvWmL3WaMn_enBk-Q@mail.gmail.com' \
    --to=ascull@google.com \
    --cc=adelva@google.com \
    --cc=bmeng.cn@gmail.com \
    --cc=keirf@google.com \
    --cc=ptosi@google.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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.