From nobody Mon Feb 9 05:59:13 2026 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 E495F47DD55 for ; Wed, 21 Jan 2026 09:36:40 +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=1768988202; cv=none; b=vGBqvVJeoYzviA6emEDJIOHKs64+HHGZ3joaAYRT3W5+oUCV5mugvK+US9k9f+xkTX3I8Uh82hxqAhc6NRM1wq+M8OZxizZYjO9c6u3HUt7LDJlGJ+f0XpS4K5yIYPdcqJjn46RQ8SOlJYj8najWknKaS7oY9U//HGnVfdFAc70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768988202; c=relaxed/simple; bh=QU6xQfM9f00ZpwjUsd6GPQ+PxU3JVUFFAWJJEmaV/o0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gPkwBtVRDy5hdvC7bvRetbVtaHI9YVV7B5S4KMJOzbvmaWoFDEaz3pTpUWupvCpoUvV4UveP63TqA+2NKYwLRETsY81YBP8zHAhOMz20MJU+jq+MVMCVrrJww96VkACY/0NtO1TmOgGshwL0iCq+0muvR3INVk2Ta2No5374kS4= 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=cdZZq3Y8; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=mBOWZvf4; 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="cdZZq3Y8"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="mBOWZvf4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768988199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7DV1XJTkbFynFHoZNtzuqUf26Hx/N2mSbX7HPLgi2JE=; b=cdZZq3Y8WETJZIDNagRnmx1CA721fBg7aSNzrfQ/sGuVCm801X+3TO9ZMAmw/HSabXoEvQ x6YmIqAc+I0jGD0RnUBe2jGBQn5DyhEvmLXkkWdGirUWwlCuPwWsVymCVC9bE18dJHg3EH vjAJHccL4tCzh0y/Ws/xymwvHv0yNxo= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-YWG4sKczMFeLlrmh_8ZMWw-1; Wed, 21 Jan 2026 04:36:38 -0500 X-MC-Unique: YWG4sKczMFeLlrmh_8ZMWw-1 X-Mimecast-MFC-AGG-ID: YWG4sKczMFeLlrmh_8ZMWw_1768988197 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-47ee868f5adso48978715e9.0 for ; Wed, 21 Jan 2026 01:36:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768988197; x=1769592997; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7DV1XJTkbFynFHoZNtzuqUf26Hx/N2mSbX7HPLgi2JE=; b=mBOWZvf4/lGHrh3XqrfKWkliWdWejnSk88BhDi+tWVE5NQjr1CO/QSXEiszUfg6+jr SuObPYERvU4XR2R1PrXsHon+dROyaSJsw2SXKDe6bFRvvGkDOl24gGXUerXdvweaRYN4 H3nMXTPTnq9pmSVM49HF2OcYrmaqHlakD7QAc5eW3AYFkcZCgxFpSqabManMkL6StVw/ hzVAuW/xzR/mAVkDfEH+aq6CVlbFvMaiuPE68BPsyaEoSCIOQ8WYxNKWLYN+TueBUzDC 6NOVQWqbjtrDHny/JB70RE1AXCPB5lWI1IL/n36qX9231+3LKO4UWrOjA+RYqZA7dNpM Eifw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768988197; x=1769592997; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=7DV1XJTkbFynFHoZNtzuqUf26Hx/N2mSbX7HPLgi2JE=; b=U+Dstpaq4yZz4N1DY9CBLlt2doSp9RHYNYGlRSB+pSrDGca4M4U1nP5mZxQh0OKxkW GoJo99qlSaSqRbgMFErye/hOQ3NjgfcrVDF21eoNMV0DE8LDccEnbwItwSdaDF/I1pT7 943RAqKMbDFMDuLOJpGC63Y1S55cF1jwytLuD6iAsBDMz8j4O+5VS5ULg29NsF7WJT1T qBVtiYFE4yC4CER3OvGMDsuMp9DJbML+iFywK2u65lIOCfvlr7V8XBEFSU7x/UKwbfr3 lBj2ZaUaE0Ic9cd0I24lJTrCjGe1wxL86L7nJBbqmXapG8M/IB+dlCg3g3ypVtAin63P DKcA== X-Forwarded-Encrypted: i=1; AJvYcCW8dSlwdMAeuyZcUlrQZIFbEDGemh8rG/XDFrPAjx1OU/oUBeBTqncBxT7VFigI9si6atvO2ESBWMXqKCQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yyc9YIbrZx5TkREfe3wfVzy0ANobf3d5TzzYCVe75YItYTo2aPD 07er9GDxiwhtWeGm53csH99RzoOMUaYN1GDQVM/Rq7p/uhxx94LwwPYxzBuPLTgzYIVprQZZpUC 2wCgPslhNP8CJ+iYkZhKIPfQri5sc4OsSvFT1odA66XHayARQEyyJ3Je8CZ1lgPw2Tw== X-Gm-Gg: AZuq6aIkB3vY40XfDZzM+Bbh/MlhNVIPPYAqKq4ndTps/eA3iYLlcdyCQxyOaSof5eC cI42ijnqjsgJLdFKAlpGS7IpgY/SZaTW6WDYjAcvHDwtB2y65Vho5x3toqiN51MwXTY2kvxTkGg M76mpWgQfkTMY9HGWh6PLeCz+6ya2WBe1QEqzM2rdmAWxRGb+laTCAiTH9lH0UCVqPVPEwa91uY 22Q/ifwZRpS5UBK6wIO9+T5gJyYdN124L1VcVYNoyNUHfttiTQ/NhaOdVxVwgABCuwXYmo0ekzs x7cBtnrPgMyIYv9mJ2y6aRR6CZj0ch5B/Ol2AfEZAyoccyLdenT3cvKOM7SvAIiOggQNd2ghs+J ae7Yc2fR8tEw6dIQzp1ynA4O4L+qfw7jbfWjKtvzGxIsMWuP+BF3K2bvB4/EL X-Received: by 2002:a05:600c:5493:b0:480:3a71:92b2 with SMTP id 5b1f17b1804b1-4803a71968bmr114132485e9.26.1768988197316; Wed, 21 Jan 2026 01:36:37 -0800 (PST) X-Received: by 2002:a05:600c:5493:b0:480:3a71:92b2 with SMTP id 5b1f17b1804b1-4803a71968bmr114131865e9.26.1768988196858; Wed, 21 Jan 2026 01:36:36 -0800 (PST) Received: from stex1.redhat.com (host-82-53-134-58.retail.telecomitalia.it. [82.53.134.58]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43595e0a6fasm7161626f8f.10.2026.01.21.01.36.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jan 2026 01:36:35 -0800 (PST) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: Simon Horman , Eric Dumazet , "Michael S. Tsirkin" , Arseniy Krasnov , "David S. Miller" , Stefano Garzarella , 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 , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Asias He , Melbin K Mathew Subject: [PATCH net v6 1/4] vsock/virtio: fix potential underflow in virtio_transport_get_credit() Date: Wed, 21 Jan 2026 10:36:25 +0100 Message-ID: <20260121093628.9941-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260121093628.9941-1-sgarzare@redhat.com> References: <20260121093628.9941-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Melbin K Mathew The credit calculation in virtio_transport_get_credit() uses unsigned arithmetic: ret =3D 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 Reviewed-by: Luigi Leonardi --- 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 @@ =20 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); =20 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_soc= k *vvs, u32 credit) return 0; =20 spin_lock_bh(&vvs->tx_lock); - ret =3D vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); - if (ret > credit) - ret =3D credit; + ret =3D min_t(u32, credit, virtio_transport_has_space(vvs)); vvs->tx_cnt +=3D ret; vvs->bytes_unsent +=3D 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); =20 -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 =3D vsk->trans; s64 bytes; =20 + /* 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 =3D (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); if (bytes < 0) bytes =3D 0; @@ -895,7 +897,7 @@ s64 virtio_transport_stream_has_space(struct vsock_sock= *vsk) s64 bytes; =20 spin_lock_bh(&vvs->tx_lock); - bytes =3D virtio_transport_has_space(vsk); + bytes =3D virtio_transport_has_space(vvs); spin_unlock_bh(&vvs->tx_lock); =20 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 =3D le32_to_cpu(hdr->buf_alloc); vvs->peer_fwd_cnt =3D le32_to_cpu(hdr->fwd_cnt); - space_available =3D virtio_transport_has_space(vsk); + space_available =3D virtio_transport_has_space(vvs); spin_unlock_bh(&vvs->tx_lock); return space_available; } --=20 2.52.0