Re: [PATCH] virtio_ring : keep used_wrap_counter in vq->last_used_idx

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jun 14, 2022 at 4:17 PM 黄杰 <huangjie.albert@xxxxxxxxxxxxx> wrote:
>
> Jason Wang <jasowang@xxxxxxxxxx> 于2022年6月14日周二 15:45写道:
> >
> > On Tue, Jun 14, 2022 at 1:38 PM Albert Huang
> > <huangjie.albert@xxxxxxxxxxxxx> wrote:
> > >
> > > From: "huangjie.albert" <huangjie.albert@xxxxxxxxxxxxx>
> > >
> > > the used_wrap_counter and the vq->last_used_idx may get
> > > out of sync if they are separate assignment,and interrupt
> > > might use an incorrect value to check for the used index.
> > >
> > > for example:OOB access
> > > ksoftirqd may consume the packet and it will call:
> > > virtnet_poll
> > >         -->virtnet_receive
> > >                 -->virtqueue_get_buf_ctx
> > >                         -->virtqueue_get_buf_ctx_packed
> > > and in virtqueue_get_buf_ctx_packed:
> > >
> > > vq->last_used_idx += vq->packed.desc_state[id].num;
> > > if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
> > >          vq->last_used_idx -= vq->packed.vring.num;
> > >          vq->packed.used_wrap_counter ^= 1;
> > > }
> > >
> > > if at the same time, there comes a vring interrupt,in vring_interrupt:
> > > we will call:
> > > vring_interrupt
> > >         -->more_used
> > >                 -->more_used_packed
> > >                         -->is_used_desc_packed
> > > in is_used_desc_packed, the last_used_idx maybe >= vq->packed.vring.num.
> > > so this could case a memory out of bounds bug.
> > >
> > > this patch is to keep the used_wrap_counter in vq->last_used_idx
> > > so we can get the correct value to check for used index in interrupt.
> > >
> > > Signed-off-by: huangjie.albert <huangjie.albert@xxxxxxxxxxxxx>
> > > ---
> > >  drivers/virtio/virtio_ring.c     | 60 ++++++++++++++++++--------------
> > >  include/uapi/linux/virtio_ring.h |  6 ++++
> > >  2 files changed, 40 insertions(+), 26 deletions(-)
> > >
> > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > > index 13a7348cedff..35c3750e89e1 100644
> > > --- a/drivers/virtio/virtio_ring.c
> > > +++ b/drivers/virtio/virtio_ring.c
> > > @@ -111,7 +111,12 @@ struct vring_virtqueue {
> > >         /* Number we've added since last sync. */
> > >         unsigned int num_added;
> > >
> > > -       /* Last used index we've seen. */
> > > +       /* Last used index  we've seen.
> > > +        * for split ring, it just contains last used index
> > > +        * for packed ring, it not only contains last used index, but also
> > > +        * used_wrap_counter, the VRING_PACKED_USED_INDEX_F_WRAP_CTR is
> > > +        * the bit shift in last_used_idx
> > > +        */
> > >         u16 last_used_idx;
> > >
> > >         /* Hint for event idx: already triggered no need to disable. */
> > > @@ -154,9 +159,6 @@ struct vring_virtqueue {
> > >                         /* Driver ring wrap counter. */
> > >                         bool avail_wrap_counter;
> > >
> > > -                       /* Device ring wrap counter. */
> > > -                       bool used_wrap_counter;
> > > -
> > >                         /* Avail used flags. */
> > >                         u16 avail_used_flags;
> > >
> > > @@ -1397,6 +1399,9 @@ static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
> > >         bool avail, used;
> > >         u16 flags;
> > >
> > > +       if (idx >= vq->packed.vring.num)
> > > +               return false;
> >
> > I wonder if we can avoid this trick, more below.
>
> Yes, the idx pass to the is_used_desc_packed can never  >=
> vq->packed.vring.num since we
> tweak the math here :
> >+       last_used += vq->packed.desc_state[id].num;
> >+       if (unlikely(last_used >= vq->packed.vring.num)) {
> >+               last_used -= vq->packed.vring.num;
> >+               used_wrap_counter ^= 1;
> >         }
> >+       last_used = (last_used | (used_wrap_counter << VRING_PACKED_DESC_F_USED));
> >+       vq->last_used_idx = last_used;
>
> >
> > > +
> > >         flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
> > >         avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
> > >         used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
> > > @@ -1406,8 +1411,12 @@ static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
> > >
> > >  static inline bool more_used_packed(const struct vring_virtqueue *vq)
> > >  {
> > > -       return is_used_desc_packed(vq, vq->last_used_idx,
> > > -                       vq->packed.used_wrap_counter);
> > > +       u16 last_used;
> > > +       bool used_wrap_counter;
> > > +
> > > +       last_used = vq->last_used_idx & ~(1 << VRING_PACKED_USED_INDEX_F_WRAP_CTR);
> > > +       used_wrap_counter = !!((vq->last_used_idx) >> VRING_PACKED_USED_INDEX_F_WRAP_CTR);
> > > +       return is_used_desc_packed(vq, last_used, used_wrap_counter);
> > >  }
> > >
> > >  static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
> > > @@ -1416,6 +1425,7 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
> > >  {
> > >         struct vring_virtqueue *vq = to_vvq(_vq);
> > >         u16 last_used, id;
> > > +       bool used_wrap_counter;
> > >         void *ret;
> > >
> > >         START_USE(vq);
> > > @@ -1434,7 +1444,8 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
> > >         /* Only get used elements after they have been exposed by host. */
> > >         virtio_rmb(vq->weak_barriers);
> > >
> > > -       last_used = vq->last_used_idx;
> > > +       used_wrap_counter = !!((vq->last_used_idx >> VRING_PACKED_USED_INDEX_F_WRAP_CTR));
> > > +       last_used = (vq->last_used_idx) & (~(1 << VRING_PACKED_USED_INDEX_F_WRAP_CTR));
> > >         id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
> > >         *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
> > >
> > > @@ -1451,12 +1462,15 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
> > >         ret = vq->packed.desc_state[id].data;
> > >         detach_buf_packed(vq, id, ctx);
> > >
> > > -       vq->last_used_idx += vq->packed.desc_state[id].num;
> > > -       if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
> > > -               vq->last_used_idx -= vq->packed.vring.num;
> > > -               vq->packed.used_wrap_counter ^= 1;
> > > +       last_used += vq->packed.desc_state[id].num;
> > > +       if (unlikely(last_used >= vq->packed.vring.num)) {
> >
> > Can we tweak the math here to avoid the out of bound result?
>
>    Yes, this can avoid the out of bound result. because of  We just
> assign last_used_idx in the following code:
>   vq->last_used_idx = last_used;
>   and it is a 16bit/32bit operations,and are guaranteed to be atomic
>
> >
> > > +               last_used -= vq->packed.vring.num;
> > > +               used_wrap_counter ^= 1;
> > >         }
> > >
> > > +       last_used = (last_used | (used_wrap_counter << VRING_PACKED_DESC_F_USED));
> > > +       vq->last_used_idx = last_used;
> > > +
> > >         /*
> > >          * If we expect an interrupt for the next entry, tell host
> > >          * by writing event index and flush out the write before
> > > @@ -1465,9 +1479,7 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
> > >         if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
> > >                 virtio_store_mb(vq->weak_barriers,
> > >                                 &vq->packed.vring.driver->off_wrap,
> > > -                               cpu_to_le16(vq->last_used_idx |
> > > -                                       (vq->packed.used_wrap_counter <<
> > > -                                        VRING_PACKED_EVENT_F_WRAP_CTR)));
> > > +                               cpu_to_le16(vq->last_used_idx));
> > >
> > >         LAST_ADD_TIME_INVALID(vq);
> > >
> > > @@ -1499,9 +1511,7 @@ static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
> > >
> > >         if (vq->event) {
> > >                 vq->packed.vring.driver->off_wrap =
> > > -                       cpu_to_le16(vq->last_used_idx |
> > > -                               (vq->packed.used_wrap_counter <<
> > > -                                VRING_PACKED_EVENT_F_WRAP_CTR));
> > > +                       cpu_to_le16(vq->last_used_idx);
> > >                 /*
> > >                  * We need to update event offset and event wrap
> > >                  * counter first before updating event flags.
> > > @@ -1518,8 +1528,7 @@ static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
> > >         }
> > >
> > >         END_USE(vq);
> > > -       return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
> > > -                       VRING_PACKED_EVENT_F_WRAP_CTR);
> > > +       return vq->last_used_idx;
> > >  }
> > >
> > >  static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
> > > @@ -1550,9 +1559,9 @@ static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
> > >         if (vq->event) {
> > >                 /* TODO: tune this threshold */
> > >                 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
> > > -               wrap_counter = vq->packed.used_wrap_counter;
> > > +               wrap_counter = !!(vq->last_used_idx >> VRING_PACKED_USED_INDEX_F_WRAP_CTR);
> > >
> > > -               used_idx = vq->last_used_idx + bufs;
> > > +               used_idx = (vq->last_used_idx & ~(1 << VRING_PACKED_USED_INDEX_F_WRAP_CTR)) + bufs;
> > >                 if (used_idx >= vq->packed.vring.num) {
> > >                         used_idx -= vq->packed.vring.num;
> > >                         wrap_counter ^= 1;
> > > @@ -1582,9 +1591,9 @@ static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
> > >          */
> > >         virtio_mb(vq->weak_barriers);
> > >
> > > -       if (is_used_desc_packed(vq,
> > > -                               vq->last_used_idx,
> > > -                               vq->packed.used_wrap_counter)) {
> > > +       wrap_counter = !!(vq->last_used_idx >> VRING_PACKED_USED_INDEX_F_WRAP_CTR);
> > > +       used_idx = (vq->last_used_idx & ~(1 << VRING_PACKED_USED_INDEX_F_WRAP_CTR));
> > > +       if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
> > >                 END_USE(vq);
> > >                 return false;
> > >         }
> > > @@ -1689,7 +1698,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > >         vq->notify = notify;
> > >         vq->weak_barriers = weak_barriers;
> > >         vq->broken = true;
> > > -       vq->last_used_idx = 0;
> > > +       vq->last_used_idx = 0 | (1 << VRING_PACKED_USED_INDEX_F_WRAP_CTR);
> > >         vq->event_triggered = false;
> > >         vq->num_added = 0;
> > >         vq->packed_ring = true;
> > > @@ -1720,7 +1729,6 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > >
> > >         vq->packed.next_avail_idx = 0;
> > >         vq->packed.avail_wrap_counter = 1;
> > > -       vq->packed.used_wrap_counter = 1;
> > >         vq->packed.event_flags_shadow = 0;
> > >         vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
> > >
> > > diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h
> > > index 476d3e5c0fe7..96bcc4d52fce 100644
> > > --- a/include/uapi/linux/virtio_ring.h
> > > +++ b/include/uapi/linux/virtio_ring.h
> > > @@ -77,6 +77,12 @@
> > >   */
> > >  #define VRING_PACKED_EVENT_F_WRAP_CTR  15
> > >
> > > +
> > > +/*
> > > + * used wrap Counter bit shift in vq->last_used_idx for packed ring
> > > + */
> > > +#define VRING_PACKED_USED_INDEX_F_WRAP_CTR  15
> >
> > Let's simply reuse VRING_PACKED_EVENT_F_WRAP_CTR ?
> >
> > Thanks
>
> Although the two values are the same, I see the meanings are different,
> so I redefine one. Is it possible to rename
> VRING_PACKED_EVENT_F_WRAP_CTR to VRING_PACKED_F_WRAP_CTR?
>
> Jason, what is your take?

That looks even worse. So I'm fine to define a new macro as 15.

Thanks

>
> >
> > > +
> > >  /* We support indirect buffer descriptors */
> > >  #define VIRTIO_RING_F_INDIRECT_DESC    28
> > >
> > > --
> > > 2.31.1
> > >
> >
>

_______________________________________________
Virtualization mailing list
Virtualization@xxxxxxxxxxxxxxxxxxxxxxxxxx
https://lists.linuxfoundation.org/mailman/listinfo/virtualization




[Index of Archives]     [KVM Development]     [Libvirt Development]     [Libvirt Users]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux