From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 973543A9DA4 for ; Wed, 21 Jan 2026 12:07:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768997246; cv=none; b=piDLJd9xyZ3HfOnL64CkwzJx9isKGWGyaeDzS2AKzIzK4MjpDHLOo2TlWe6fWdpyrYk7RRDqFylydZaG7gtdnfIpupen9GYbotDjAXY/8xwLD49RTjh9n52w3VCke0Zl3iDL+KaNk+FoZYynTc3OCZUWjc8a2U7D4PgIiBUX2V0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768997246; c=relaxed/simple; bh=sRJH0LcPiGEMiEKO6ssQ7+EoDIhVQvOD4UKf23OY3vc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uusR4KndNIaWZOC9uCWwsSeuOfkhgscb0A58/uEBwDG98BgQUaPybbLLAq5h5ZOiV26GapMPwpKmpVd/zm0JZbtbZOBPS8xLAuMu1FwDaXrif9HKdHkvbsLwZBk5a4ysfp7q7STvd+TsG+uD8Lc7kfRxatop1JUINt3j73ySc+Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UoZ9WNtn; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=TT4lfn2s; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UoZ9WNtn"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="TT4lfn2s" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768997243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=dRcVrBqhQujwyY3t8+ooFMd21puB/4S+X1pfO3AW+ug=; b=UoZ9WNtnzrZj/fh35Z+uNAFJO3PYsu5hB3DfMitohaFUmAsz7bfgRaROfPwQsoci3rZNsB UIwJxkoAR7z3QB/Rpm7X1BLIfBF2kuSUilheNzz62KoakLbL04gFGy2m2dugBVophJ4rXB wWjVBjycB8JdYcpQrnprBIrcLMcOXEY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-12-vz9GlnR0NqCcU-lrD9uZJA-1; Wed, 21 Jan 2026 07:07:22 -0500 X-MC-Unique: vz9GlnR0NqCcU-lrD9uZJA-1 X-Mimecast-MFC-AGG-ID: vz9GlnR0NqCcU-lrD9uZJA_1768997241 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-48025e12b5bso38796755e9.2 for ; Wed, 21 Jan 2026 04:07:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768997241; x=1769602041; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=dRcVrBqhQujwyY3t8+ooFMd21puB/4S+X1pfO3AW+ug=; b=TT4lfn2sSGIKKmGEFjXuJ+gDHTb4eBFDlCoigiVvx4ghQtF6sx/ehFf+TaiH+C85X6 ufbuZYdyiGHR9WK9+vbYoLBP6btx2S/eUnK1J6TP2jPRwiCwpmgJMkoj53mxxIC4BT+y Tyu4vwfv9rH+eZHaSMImnCkoKW5oNlJgTuRhvbqyYUVRY2tb+tj//Xz0B0NllXpV/wD7 ESc+l2Tu922YtRvmNXXeGUg8ll2qPoY7HUX8QgGlxtaMdaMjYq+r0FFTt2hW+gkPd6O7 cRx2Iu06Q1zYPvnmF+wqTlPlPe+yWofTOPPEC17fET6VqXfLDm46rTlUkdb3J7IpuMPJ /kVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768997241; x=1769602041; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dRcVrBqhQujwyY3t8+ooFMd21puB/4S+X1pfO3AW+ug=; b=CGlHtN6d99L8lQCbJfRHWT7Kka3Ws4PlfUS79sFpMnS8Zun4u+Vkcj+TaOG78ms5mU lTIIqzmMWyU0cBhd0qrAOxhLw8RX3h88kmU7r7RH+6rcVK8r+L7MvfPqW26VAjCVYjxX B2IrPQHtPsnpXxn2/117jnb8zuJWrnCykSH4SYHchtp6v8iz7JpNVEjwy5PtIPemTeEl Xcf3KLFZV825PuAlMjS9On3NsJ6Fjfe/Vze8N4La4+hVULxrsQ3GcbTlGxvXF1nBuNOS 2vOA3vgEY53VI+ip+YVLW5cYv6t0Krz1TDA3GT4b/DK5mDrItUchGEyrWtqWGC6RBO5i SZRw== X-Forwarded-Encrypted: i=1; AJvYcCVcH1UxNz8wG/zWpzIFI6IQxpEoeAqTOEWOABoJ9TZMJff6wdoaru7Xi+l3no/0nAQIBPM=@vger.kernel.org X-Gm-Message-State: AOJu0YzGguc/plHcm9/a+Hj7FA4/y6BbBQiwfjARzf3ACNEcQNz/mjmW ezvJbBvH5R9mKxJjDnSsD3mDDXu1jBRyGi+VkHho/umDkKgE7aky+MkrY2LNdsiyWumEQYsSjUX v6bwSPnHfhN5jnatxPuzJR1a2PliGVWovzzrdiuji7cOQLA26CF33+g== X-Gm-Gg: AZuq6aKWhNXzDE5m6g0X29E0yWmj+QlREfqiKrIyzm3mFSOYZhT9IEzR9VWntaznOJo CeTQcmmgoFtHJLOrXzt6HjEPVnZT3A1oW8C4Bt48/in6SZjq5F7VexJ/K5ChsjZa71sIZZgUHeg sRMnZtYSvLWMm412AhMztHbmo6kK1U283zkFAFG+AHxA2pClvijvHEOFDzE/8k21qlwgj3auVuU 3XK2jkqWyWyOwaiKPhylqJBWBvYIoKoHQkP06hUX4hA3nI2hzb5Shp1pWC2imhk0tZ07SKeRsLO mgTUP2d/EATbNLnkROw40PkX/bv8HDVYM8mUODBUf2vGjXRt8AQDV/ocbSDBmqD4Y4hK5hYx9lI 6TzsazQHzH84R3k4= X-Received: by 2002:a05:600c:3e0e:b0:480:3ad0:6509 with SMTP id 5b1f17b1804b1-4803e7a2da0mr80803005e9.12.1768997240987; Wed, 21 Jan 2026 04:07:20 -0800 (PST) X-Received: by 2002:a05:600c:3e0e:b0:480:3ad0:6509 with SMTP id 5b1f17b1804b1-4803e7a2da0mr80802465e9.12.1768997240572; Wed, 21 Jan 2026 04:07:20 -0800 (PST) Received: from leonardi-redhat ([176.206.16.134]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-480424a37dasm22002135e9.2.2026.01.21.04.07.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jan 2026 04:07:20 -0800 (PST) Date: Wed, 21 Jan 2026 13:07:17 +0100 From: Luigi Leonardi To: Stefano Garzarella Cc: netdev@vger.kernel.org, Simon Horman , Eric Dumazet , "Michael S. Tsirkin" , Arseniy Krasnov , "David S. Miller" , virtualization@lists.linux.dev, Paolo Abeni , Jakub Kicinski , Stefan Hajnoczi , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Claudio Imbrenda , Jason Wang , Xuan Zhuo , Eugenio =?utf-8?B?UMOpcmV6?= , Asias He , Melbin K Mathew Subject: Re: [PATCH net v6 1/4] vsock/virtio: fix potential underflow in virtio_transport_get_credit() Message-ID: References: <20260121093628.9941-1-sgarzare@redhat.com> <20260121093628.9941-2-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20260121093628.9941-2-sgarzare@redhat.com> On Wed, Jan 21, 2026 at 10:36:25AM +0100, Stefano Garzarella wrote: >From: Melbin K Mathew > >The credit calculation in virtio_transport_get_credit() uses unsigned >arithmetic: > > ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); > >If the peer shrinks its advertised buffer (peer_buf_alloc) while bytes >are in flight, the subtraction can underflow and produce a large >positive value, potentially allowing more data to be queued than the >peer can handle. > >Reuse virtio_transport_has_space() which already handles this case and >add a comment to make it clear why we are doing that. > >Fixes: 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") >Suggested-by: Stefano Garzarella >Signed-off-by: Melbin K Mathew >[Stefano: use virtio_transport_has_space() instead of duplicating the code] >[Stefano: tweak the commit message] >Signed-off-by: Stefano Garzarella >--- > net/vmw_vsock/virtio_transport_common.c | 16 +++++++++------- > 1 file changed, 9 insertions(+), 7 deletions(-) > >diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c >index 26b979ad71f0..6175124d63d3 100644 >--- a/net/vmw_vsock/virtio_transport_common.c >+++ b/net/vmw_vsock/virtio_transport_common.c >@@ -28,6 +28,7 @@ > > static void virtio_transport_cancel_close_work(struct vsock_sock *vsk, > bool cancel_timeout); >+static s64 virtio_transport_has_space(struct virtio_vsock_sock *vvs); > > static const struct virtio_transport * > virtio_transport_get_ops(struct vsock_sock *vsk) >@@ -499,9 +500,7 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) > return 0; > > spin_lock_bh(&vvs->tx_lock); >- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); >- if (ret > credit) >- ret = credit; >+ ret = min_t(u32, credit, virtio_transport_has_space(vvs)); > vvs->tx_cnt += ret; > vvs->bytes_unsent += ret; > spin_unlock_bh(&vvs->tx_lock); >@@ -877,11 +876,14 @@ u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk) > } > EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data); > >-static s64 virtio_transport_has_space(struct vsock_sock *vsk) >+static s64 virtio_transport_has_space(struct virtio_vsock_sock *vvs) > { >- struct virtio_vsock_sock *vvs = vsk->trans; > s64 bytes; > >+ /* Use s64 arithmetic so if the peer shrinks peer_buf_alloc while >+ * we have bytes in flight (tx_cnt - peer_fwd_cnt), the subtraction >+ * does not underflow. >+ */ > bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); > if (bytes < 0) > bytes = 0; >@@ -895,7 +897,7 @@ s64 virtio_transport_stream_has_space(struct vsock_sock *vsk) > s64 bytes; > > spin_lock_bh(&vvs->tx_lock); >- bytes = virtio_transport_has_space(vsk); >+ bytes = virtio_transport_has_space(vvs); > spin_unlock_bh(&vvs->tx_lock); > > return bytes; >@@ -1492,7 +1494,7 @@ static bool virtio_transport_space_update(struct sock *sk, > spin_lock_bh(&vvs->tx_lock); > vvs->peer_buf_alloc = le32_to_cpu(hdr->buf_alloc); > vvs->peer_fwd_cnt = le32_to_cpu(hdr->fwd_cnt); >- space_available = virtio_transport_has_space(vsk); >+ space_available = virtio_transport_has_space(vvs); > spin_unlock_bh(&vvs->tx_lock); > return space_available; > } >-- 2.52.0 > LGTM! Reviewed-by: Luigi Leonardi