* [PATCH] vhost: reject used ring batches larger than the ring
@ 2026-12-01 22:18 Michael S. Tsirkin
2026-12-02 8:15 ` Eugenio Perez Martin
2026-12-02 17:58 ` Dongli Zhang
0 siblings, 2 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2026-12-01 22:18 UTC (permalink / raw)
To: linux-kernel
Cc: stable, Seohyeon Maeng, minki, YanXin Li, Jason Wang,
Eugenio Pérez, David S. Miller, Linfeng Sun, kvm,
virtualization, netdev
A compliant driver never has more than vq->num buffers in flight at
once, so a batch accumulated before used->idx advances never exceeds
vq->num.
Reject used count > vq->num in
vhost_add_used_n()/vhost_add_used_n_in_order().
Cc: stable@vger.kernel.org
Reported-by: Seohyeon Maeng <bioloidgp@gmail.com>
Reported-by: minki@morningstarsec.dev
Reported-by: YanXin Li <fadouse@pm.me>
Fixes: e2b3b35eb989 ("vhost_net: batch used ring update in rx")
Fixes: 4afb52c2af44 ("vhost_net: batch update used ring for datacopy TX")
Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
Cc: Jason Wang <jasowangio@gmail.com>
Cc: Eugenio Pérez <eperezma@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/vhost/vhost.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index d1831f4e09981..a00151932e4e5 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -3181,6 +3181,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
{
vring_used_elem_t __user *used;
u16 old, new = vq->last_used_idx;
+ unsigned int total = 0;
int start, i;
if (!nheads)
@@ -3189,6 +3190,12 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
start = vq->last_used_idx & (vq->num - 1);
for (i = 0; i < count; i++) {
+ total += nheads[i];
+ if (unlikely(total > vq->num)) {
+ vq_err(vq, "Added %u > %u used heads\n",
+ total, vq->num);
+ return -EINVAL;
+ }
if (vhost_put_used(vq, &heads[i], start, 1)) {
vq_err(vq, "Failed to write used");
return -EFAULT;
@@ -3245,6 +3252,11 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
int r;
+ if (unlikely(count > vq->num)) {
+ vq_err(vq, "Added %u > %u used entries\n", count, vq->num);
+ return -EINVAL;
+ }
+
if (!in_order || !nheads)
r = vhost_add_used_n_ooo(vq, heads, count);
else
--
MST
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] vhost: reject used ring batches larger than the ring
2026-12-01 22:18 [PATCH] vhost: reject used ring batches larger than the ring Michael S. Tsirkin
@ 2026-12-02 8:15 ` Eugenio Perez Martin
2026-12-02 8:33 ` Michael S. Tsirkin
2026-12-02 17:58 ` Dongli Zhang
1 sibling, 1 reply; 4+ messages in thread
From: Eugenio Perez Martin @ 2026-12-02 8:15 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: linux-kernel, stable, Seohyeon Maeng, minki, YanXin Li,
Jason Wang, David S. Miller, Linfeng Sun, kvm, virtualization,
netdev
On Tue, Dec 1, 2026 at 11:19 PM Michael S. Tsirkin <mst@redhat.com> wrote:
>
> A compliant driver never has more than vq->num buffers in flight at
> once, so a batch accumulated before used->idx advances never exceeds
> vq->num.
>
> Reject used count > vq->num in
> vhost_add_used_n()/vhost_add_used_n_in_order().
>
> Cc: stable@vger.kernel.org
> Reported-by: Seohyeon Maeng <bioloidgp@gmail.com>
> Reported-by: minki@morningstarsec.dev
> Reported-by: YanXin Li <fadouse@pm.me>
> Fixes: e2b3b35eb989 ("vhost_net: batch used ring update in rx")
> Fixes: 4afb52c2af44 ("vhost_net: batch update used ring for datacopy TX")
> Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
> Cc: Jason Wang <jasowangio@gmail.com>
> Cc: Eugenio Pérez <eperezma@redhat.com>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> drivers/vhost/vhost.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index d1831f4e09981..a00151932e4e5 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -3181,6 +3181,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
> {
> vring_used_elem_t __user *used;
> u16 old, new = vq->last_used_idx;
> + unsigned int total = 0;
> int start, i;
>
> if (!nheads)
> @@ -3189,6 +3190,12 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
> start = vq->last_used_idx & (vq->num - 1);
>
> for (i = 0; i < count; i++) {
> + total += nheads[i];
> + if (unlikely(total > vq->num)) {
> + vq_err(vq, "Added %u > %u used heads\n",
> + total, vq->num);
> + return -EINVAL;
> + }
Putting the check here returns -EINVAL, but some buffers have
effectively been sent if the for loop ran any iterations. But the
caller cannot know how many buffers have been sent. Perhaps the check
should occur before the for loop, ensuring an all-or-nothing
operation?
The caller is already broken if it performs this operation so this is
a little nitpick, but recovery is easier that way.
> if (vhost_put_used(vq, &heads[i], start, 1)) {
> vq_err(vq, "Failed to write used");
> return -EFAULT;
> @@ -3245,6 +3252,11 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
> bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
> int r;
>
> + if (unlikely(count > vq->num)) {
> + vq_err(vq, "Added %u > %u used entries\n", count, vq->num);
> + return -EINVAL;
> + }
> +
> if (!in_order || !nheads)
> r = vhost_add_used_n_ooo(vq, heads, count);
> else
> --
> MST
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] vhost: reject used ring batches larger than the ring
2026-12-02 8:15 ` Eugenio Perez Martin
@ 2026-12-02 8:33 ` Michael S. Tsirkin
0 siblings, 0 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2026-12-02 8:33 UTC (permalink / raw)
To: Eugenio Perez Martin
Cc: linux-kernel, stable, Seohyeon Maeng, minki, YanXin Li,
Jason Wang, David S. Miller, Linfeng Sun, kvm, virtualization,
netdev
On Wed, Dec 02, 2026 at 09:15:43AM +0100, Eugenio Perez Martin wrote:
> On Tue, Dec 1, 2026 at 11:19 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > A compliant driver never has more than vq->num buffers in flight at
> > once, so a batch accumulated before used->idx advances never exceeds
> > vq->num.
> >
> > Reject used count > vq->num in
> > vhost_add_used_n()/vhost_add_used_n_in_order().
> >
> > Cc: stable@vger.kernel.org
> > Reported-by: Seohyeon Maeng <bioloidgp@gmail.com>
> > Reported-by: minki@morningstarsec.dev
> > Reported-by: YanXin Li <fadouse@pm.me>
> > Fixes: e2b3b35eb989 ("vhost_net: batch used ring update in rx")
> > Fixes: 4afb52c2af44 ("vhost_net: batch update used ring for datacopy TX")
> > Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
> > Cc: Jason Wang <jasowangio@gmail.com>
> > Cc: Eugenio Pérez <eperezma@redhat.com>
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> > drivers/vhost/vhost.c | 12 ++++++++++++
> > 1 file changed, 12 insertions(+)
> >
> > diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> > index d1831f4e09981..a00151932e4e5 100644
> > --- a/drivers/vhost/vhost.c
> > +++ b/drivers/vhost/vhost.c
> > @@ -3181,6 +3181,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
> > {
> > vring_used_elem_t __user *used;
> > u16 old, new = vq->last_used_idx;
> > + unsigned int total = 0;
> > int start, i;
> >
> > if (!nheads)
> > @@ -3189,6 +3190,12 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
> > start = vq->last_used_idx & (vq->num - 1);
> >
> > for (i = 0; i < count; i++) {
> > + total += nheads[i];
> > + if (unlikely(total > vq->num)) {
> > + vq_err(vq, "Added %u > %u used heads\n",
> > + total, vq->num);
> > + return -EINVAL;
> > + }
>
> Putting the check here returns -EINVAL, but some buffers have
> effectively been sent if the for loop ran any iterations. But the
> caller cannot know how many buffers have been sent. Perhaps the check
> should occur before the for loop, ensuring an all-or-nothing
> operation?
That's an extra loop, and the driver is out of spec. Not worth the perf
cost I'd say?
> The caller is already broken if it performs this operation so this is
> a little nitpick, but recovery is easier that way.
Recovery is mostly reset anyway.
>
> > if (vhost_put_used(vq, &heads[i], start, 1)) {
> > vq_err(vq, "Failed to write used");
> > return -EFAULT;
> > @@ -3245,6 +3252,11 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
> > bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
> > int r;
> >
> > + if (unlikely(count > vq->num)) {
> > + vq_err(vq, "Added %u > %u used entries\n", count, vq->num);
> > + return -EINVAL;
> > + }
> > +
> > if (!in_order || !nheads)
> > r = vhost_add_used_n_ooo(vq, heads, count);
> > else
> > --
> > MST
> >
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] vhost: reject used ring batches larger than the ring
2026-12-01 22:18 [PATCH] vhost: reject used ring batches larger than the ring Michael S. Tsirkin
2026-12-02 8:15 ` Eugenio Perez Martin
@ 2026-12-02 17:58 ` Dongli Zhang
1 sibling, 0 replies; 4+ messages in thread
From: Dongli Zhang @ 2026-12-02 17:58 UTC (permalink / raw)
To: Michael S. Tsirkin, linux-kernel
Cc: stable, Seohyeon Maeng, minki, YanXin Li, Jason Wang,
Eugenio Pérez, David S. Miller, Linfeng Sun, kvm,
virtualization, netdev
On Tue, Dec 1, 2026 2:18:53PM -0800, Michael S. Tsirkin wrote:
> A compliant driver never has more than vq->num buffers in flight at
> once, so a batch accumulated before used->idx advances never exceeds
> vq->num.
>
> Reject used count > vq->num in
> vhost_add_used_n()/vhost_add_used_n_in_order().
Only regarding the out_out_order case, may I confirm if this patch is still
required as the below one is already available?
vhost: split out-of-order used ring batches at every wrap
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5570a998b884c988dbe105430d7e2d8e9411e37e
Thank you very much!
Dongli Zhang
>
> Cc: stable@vger.kernel.org
> Reported-by: Seohyeon Maeng <bioloidgp@gmail.com>
> Reported-by: minki@morningstarsec.dev
> Reported-by: YanXin Li <fadouse@pm.me>
> Fixes: e2b3b35eb989 ("vhost_net: batch used ring update in rx")
> Fixes: 4afb52c2af44 ("vhost_net: batch update used ring for datacopy TX")
> Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
> Cc: Jason Wang <jasowangio@gmail.com>
> Cc: Eugenio Pérez <eperezma@redhat.com>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> drivers/vhost/vhost.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index d1831f4e09981..a00151932e4e5 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -3181,6 +3181,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
> {
> vring_used_elem_t __user *used;
> u16 old, new = vq->last_used_idx;
> + unsigned int total = 0;
> int start, i;
>
> if (!nheads)
> @@ -3189,6 +3190,12 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
> start = vq->last_used_idx & (vq->num - 1);
>
> for (i = 0; i < count; i++) {
> + total += nheads[i];
> + if (unlikely(total > vq->num)) {
> + vq_err(vq, "Added %u > %u used heads\n",
> + total, vq->num);
> + return -EINVAL;
> + }
> if (vhost_put_used(vq, &heads[i], start, 1)) {
> vq_err(vq, "Failed to write used");
> return -EFAULT;
> @@ -3245,6 +3252,11 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
> bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
> int r;
>
> + if (unlikely(count > vq->num)) {
> + vq_err(vq, "Added %u > %u used entries\n", count, vq->num);
> + return -EINVAL;
> + }
> +
> if (!in_order || !nheads)
> r = vhost_add_used_n_ooo(vq, heads, count);
> else
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 17:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 22:18 [PATCH] vhost: reject used ring batches larger than the ring Michael S. Tsirkin
2026-12-02 8:15 ` Eugenio Perez Martin
2026-12-02 8:33 ` Michael S. Tsirkin
2026-12-02 17:58 ` Dongli Zhang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox