From nobody Mon Feb 9 08:27:54 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 0FDCB2F3624 for ; Fri, 16 Jan 2026 20:15:27 +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=1768594529; cv=none; b=MPmyjbILX18e9hf0YBK3k+H66Tz/1Hnb1OSHc6W1TEOECB4wKlM0MhHEMZtpjZCHaS9by/eQlVjoa+AYsII9ZXKZ9EEvv7SR7xTHjXIhT4CuCfq29oI68Mxze+wP4mG6eUPAPAOjC4Xn7i+/wETUqcli2DektD9s2JUWqS/gudc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768594529; c=relaxed/simple; bh=A/Z6X724OpOjH0JR1tZITqCXaxlg20lb95ngLUoUj2w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BE3V5QXOhT0fWX1tnShr+it1OZAKbH3hBJm+1sebQUzNb7ZnUaqVB2V6X8mb/LSpuO8wdO+uIpodkac94dwm4v8vubIE7NfWnLMQ3rNLE7zwakZGkK2Nq75td47lgTJNoazGuGi/jOcYbmEidGUWTBHqAWHLb3ZILtULnCggPkk= 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=BqIlU4e8; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=iu9kQ+IS; 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="BqIlU4e8"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="iu9kQ+IS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768594527; 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=ArUbsQ+kNBjAUv+ApSXUG5Hd0kEcrS3qPgkd0OoiEwI=; b=BqIlU4e82bIfs0nNr/FeiPY7FvXZwJ8uf55JDQFM6xAKqp65X3cR3uJMOh5jBcH0Rliype Z1umZ0W060pizUpuFFEU76OhEwgauRkTQig9D52IWdupjymbeKJAwyvW4BKoPrRig0JQur F6yPvoPb9JHjuk62QPY0YCeM1VraKTs= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-426-1dsDDDI8Mbmj9ilnBYajLg-1; Fri, 16 Jan 2026 15:15:26 -0500 X-MC-Unique: 1dsDDDI8Mbmj9ilnBYajLg-1 X-Mimecast-MFC-AGG-ID: 1dsDDDI8Mbmj9ilnBYajLg_1768594525 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-47ed980309aso17488245e9.0 for ; Fri, 16 Jan 2026 12:15:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768594525; x=1769199325; 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=ArUbsQ+kNBjAUv+ApSXUG5Hd0kEcrS3qPgkd0OoiEwI=; b=iu9kQ+ISQWJqsK5FtvBvvLkJHqRuxrbRBbDMK9HHXNpRKYVDbZqDkGT/pgvbQWDgp0 VtxfTWp5E3P5F+tGuFQMQIXUKWPx1SYqD2HH1FwNUmGYGfADPc/rzgPcxEGolpoq9DUD rOBrtOaRMDRt2Pm7igYdm2L92CAcg+A9Xij6Lu5e1GYjAsUJKnbSIiJ/mqSKv2F2cv4s xeUDIiWvgoc87MhPgd5WFaNrM2VcXgh/QiFi5+R4hbZXFKyPcOSh4b5MD+Qo+MkHGaxA qzEdokb5DfEzSg6x8D66yielR38HC05Kw6UevKqJmsR4K1Wr6em5Rv0IYBOxlxVxiRRD d7ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768594525; x=1769199325; 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=ArUbsQ+kNBjAUv+ApSXUG5Hd0kEcrS3qPgkd0OoiEwI=; b=L73xuy2NJbst2DM/G3SFhNNHh06rleZeGAJlWQLav6eZWQHshF7gguPggWLSpVdJS4 r5qwO8jPFIAW5mJiGx5BY/8FRyqRb/kWGbMO7xKWRnX2I43Hd+ajjW1yL7IE1utV5W1C Ca7GbzzWW3oxtNkQfDl5gk7VKGmiF0GKx6hn/dis2FXGd0JwAuOXTh5H4ENL3lwxsOda +LFPoU4VafjfQ7B+yC0ZCcyAiXuKo3r1kqEW9XwUmhzz5yDljFi53X6Z9HipcK7l4QbS 4/FTX7XLGh7QiFtAf99WMLVAu/YYrZJJSwN0OAh0U7lRv+IOzrmzdEw5pEG9T9U9yfOK x1yA== X-Forwarded-Encrypted: i=1; AJvYcCW7xc8WxgISYt/XxOFogKXmOkEtlloVEdEo9fzECj6E9v1udOWqELXR3JOlAIomuyJxMOfe/+c4AqwFxws=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6Sv53LsRyIRrz8UDIt0DX14n3lW+3u5jkihRz1OBkQedPRfiX 7b/3J1mlurlOEQnaBxnzNb5oQpos2tOvIqwyZw5bGubho6eFVY2pOonug0T6BotZ6JkREBLFKGe S0RP9c7Yg1yifNxsGs6qmykX3ZoVI0PqNshnZfL7o1W9XakzVXtCWrDzQzS94dqu65Q== X-Gm-Gg: AY/fxX7OYPLJQwb0hEIQeLYQQyy7VPpBTLdEkroR1AHyuhjw9NCg6ya238Nngx0MBkJ FMLwtFpe1c6f95PZG/LjpOO/oDj6EU1CUPu0CRZlcHMDEQzC0oDkq+C72mD2BCarguRuCKvF4c0 bLtAktZRMo6hqqh5PLvdQ7/5tUy3qnRPLyilFpuVS/d/hfPNbZU67v1ZylWxxmbdvwZ11/nYd7g bW5CfS2xMJnJzyX2CLEdHX2Hfo3/+t24LjQKu0Vi2TU5qlzWLdFU5ET9piO4TaJAXSfh0ISDdJP FIosxLj79I9pyJrIZUYis4GLRH/H8AHJjJBRHxH65bF5vgM5R7+nXdQR+t/Km5ulA0qcFYuJ9ra cUR07PstJ6V/9IcdM/FfOCUDiQFyJ62CeQZKASDocaS0yv6iX+09fcHFVrv1+ X-Received: by 2002:a05:600c:1f86:b0:477:a289:d854 with SMTP id 5b1f17b1804b1-4801e53ca36mr57538805e9.5.1768594524938; Fri, 16 Jan 2026 12:15:24 -0800 (PST) X-Received: by 2002:a05:600c:1f86:b0:477:a289:d854 with SMTP id 5b1f17b1804b1-4801e53ca36mr57538465e9.5.1768594524424; Fri, 16 Jan 2026 12:15:24 -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 5b1f17b1804b1-4801e879537sm57802005e9.5.2026.01.16.12.15.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jan 2026 12:15:23 -0800 (PST) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: virtualization@lists.linux.dev, "Michael S. Tsirkin" , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Stefan Hajnoczi , Paolo Abeni , kvm@vger.kernel.org, Eric Dumazet , linux-kernel@vger.kernel.org, Jason Wang , Claudio Imbrenda , Simon Horman , "David S. Miller" , Arseniy Krasnov , Asias He , Stefano Garzarella , Jakub Kicinski , Xuan Zhuo , Melbin K Mathew Subject: [PATCH RESEND net v5 1/4] vsock/virtio: fix potential underflow in virtio_transport_get_credit() Date: Fri, 16 Jan 2026 21:15:14 +0100 Message-ID: <20260116201517.273302-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260116201517.273302-1-sgarzare@redhat.com> References: <20260116201517.273302-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 --- 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 dcc8a1d5851e..2fe341be6ce2 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; @@ -1490,7 +1492,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