🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #5777

[PATCH] vhost: fix used ring index wrap in vhost_add_used_n_in_order

#5777
From: Fang Xieyan
Date: Tue, 20 Oct 2026 23:29
54 lines
10290 bytes
vhost_add_used_n_in_order() advances start by nheads[i] but wraps it
with a single subtraction.  If start + nheads[i] >= 2 * vq->num, start
can remain outside the ring.

With used-ring logging enabled, start > vq->num makes (vq->num - start)
underflow.  log_write() then sets bits beyond the dirty-log bitmap,
potentially corrupting adjacent userspace memory.  This is reachable on
the vhost-net TX path with VIRTIO_F_IN_ORDER: done_idx can reach
VHOST_NET_BATCH (64), triggering the issue with a 32-entry ring.

Wrap start with a mask to keep it within the ring.  VHOST_SET_VRING_NUM
enforces a nonzero power-of-two ring size, allowing start to be wrapped
using & (vq->num - 1).

Fixes: 67a873df0c41 ("vhost: basic in order support")
Cc: stable@vger.kernel.org
Reviewed-by: Weibin Liu <liuwb@xiaopeng.com>
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
Reproducer (verified on a kernel containing commit 67a873df0c41 with a
temporary WARN_ON_ONCE probe in log_used(); the probe is not part of
this patch):

With VIRTIO_F_IN_ORDER, num=32, and used-ring dirty logging enabled, the
TX batch reaches done_idx=64.  In the observed run, the initial index is
3, so the single subtraction leaves start=35.  The resulting length
calculation wraps to 34359738344 bytes, as confirmed by the log_used()
probe.

A 64-byte canary immediately after the 512-byte log bitmap changes from
0xA5 to 0xFF, confirming the out-of-bounds write.  The used-ring canary
remains intact because vhost_put_used() runs before start is advanced;
the demonstrated corruption is in the dirty-log bitmap.

 drivers/vhost/vhost.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 44cac11b68d2..0226c4c59709 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -3172,8 +3172,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
 		}
 		start += nheads[i];
 		new += nheads[i];
-		if (start >= vq->num)
-			start -= vq->num;
+		start &= vq->num - 1;
 	}
 
 	if (unlikely(vq->log_used)) {
-- 
2.50.1
Technical Details
Message-ID: <20261020232956.99970-1-fangxy@xiaopeng.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-349646-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-349646-linux-kernel-netdev=dewy.ru
From: fangxy@xiaopeng.com (Fang Xieyan)
Newsgroups: linux.kernel.netdev
Subject: [PATCH] vhost: fix used ring index wrap in vhost_add_used_n_in_order
Date: Wed, 21 Oct 2026 07:29:56 +0800
Organization: Usenet Network
Message-ID: <20261020232956.99970-1-fangxy@xiaopeng.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3993004"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:kdUVIuCi5/QLuc8cI5q1t2tncCI= sha256:SCoMxC6QqUVpg+6+XumbKTkiVxdPnmbQWgd2d5egsyw=
	sha1:4W4oE2Y5C7fPkZtylkvsBeRaggg= sha256:jWhycZ0JXEj/+SZ6BXm+eQ8/ncxyxV0sNzGuJ4h9RsE=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id D1FBD110E9F; Wed, 21 Oct 2026 02:30:36 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, URIBL_BLOCKED=0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=xiaopeng.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id anYhw1RbjZ2t;
	Wed, 21 Oct 2026 02:30:33 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-349646-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=xiaopeng.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 55407110E9A; Wed, 21 Oct 2026 02:30:24 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 5F0AD37B0C; Tue, 20 Oct 2026 23:30:16 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1792539015;
	b=eOukN46xdRptt4JVoLcnnLXfHBpxXBXrf7IUU0BEqVzL2XuzDHhCP9DwGcSKWR6WCUX6
	 P2kTTM9gHwj0rSgoumK7TipQRaccoAM7Ma2U5llueTUa5NC3l92mnFK+r3+Dgju1juV3g
	 Wb6E/t5h8blzs/+Chs5CC7lwK0EfypiIzgVkvTbfs4EIRfAsVU9ThhKRfYzQgju6MsoOX
	 hrPUvB5sV3WZkuKm688nQV14/19xKztRYF6G2FmlyeZIA9z4azpNN+RtgdpovZza8ZmNN
	 fDHhYZgR6/SCsjapqJK3NqVz50B90TEyjMuXqio1LVZleiPtIZYOr1+ztr4ixea24Ug==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792539015;
	h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:
	 X-Alimail-AntiSpam:Received:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailer:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=vpOI+1qBW/MtfJm0O85kyO88qqkrY/yJqDCLaRNjbVw=;
	b=avYZH3Schwgkap2lSpH8lWaJCfBdGoNiTUDTiF+ovPRj8WAYBNZYYqWX1aGs7JtkDpEb
	 C+/HMCucMvWskl/ikHG5drNdmNwWEyhcYDyCOkZ8iMXRYVcrkh+iUPnpbz9P0nqe7gvji
	 TwdaFaqosq1EEoHXkLGXDc6p/k87uob98yJwLiWnjdQ2igjgoqU/UutX0Y+lBSNdhQrRR
	 AQgNm1IKjIB/wXD85G9Cd5997MWwhyn7zpAojm6GrfzQiS3S98rW/UKOCR03jHclheNfJ
	 Yd5zSuioxekrVRoACJZtCXpa7nsJ1y0LKQ5BqOCq8PX3iUew0Jf4eVDBi0BxZiWGaTA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=QGACHvRG;
	dmarc=none header.from=xiaopeng.com;
	spf=pass smtp.mailfrom=xiaopeng.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 638C6606A328;
	Tue, 20 Oct 2026 23:30:15 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=QGACHvRG
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.148
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1792539004;
	b=GWYoNt+feswy5p75FW/udWJvkq4NhoWYAQW3kc2oIDhMgSn1eIXGAFiT9yP/kcQtDK+B
	 RH5orWzmjfpUzFRVbVYA6EwEkLfJnZXWOAus/8WT1BgdskwlIg3KV1jtSUFk0Ruli1kea
	 UmKG9GLpYVDc2B1LEbCe5EzalcFZtTNakQDEt8+JAiFbj3OAXuEhAwBZiljSX8yzVfL4I
	 XLXrz272VXJUSFPDehiIuBQdlHVflGd9UIGGAOiRAMkxQSt57xDhBb0EyQ2uZ+TEd03jk
	 cT9DqSsiIydFVMCzjnpfQAVI45F/K0rnh74wPETI/F1LZt2/6aa2JyCqC0/7x+GpaoA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792539004;
	h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc:
	 Subject:Date:Message-ID:X-Mailer:MIME-Version:
	 Content-Transfer-Encoding;
	bh=vpOI+1qBW/MtfJm0O85kyO88qqkrY/yJqDCLaRNjbVw=;
	b=VRN45EG5x2n7LZmtNevFSlWK2eQve2XBAAJ8fNKnYX5HzpQ+Yk2IUamQ9fS57fkggPXU
	 hOhIql5D2oaSUVX5GBRXS0sc+il2eXkr5KZeheQmJ7/rn7oCeGAIX+AvCOZn3sI5NkmFm
	 ciZHkoFbEubGEqoFZOuaoyJJ3G91voCbP7gHDd2PD+/kCTbpeYLNKAmylXLDDCErFzFzE
	 v1mR7Okj5uoF0dhSyMABndPDkx/NCNCAS/nAoqdMoI1A6cEtpbdreYgDiEcL9OUkLcnR9
	 fw0mNevg/XY0rlYbFnuJA+qJu7SBMhbQdPsglzm5gfetgOpfaNsvVsWk6XjrgcP76Tw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=xiaopeng.com;
	spf=pass smtp.mailfrom=xiaopeng.com;
	dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=QGACHvRG;
	arc=none smtp.remote-ip=115.124.28.148
X-Received: from out28-148.mail.aliyun.com (out28-148.mail.aliyun.com [115.124.28.148])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0A8A606A320;
	Tue, 20 Oct 2026 23:30:01 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A0A8A606A320
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xiaopeng.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xiaopeng.com
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
	d=xiaopeng.com; s=default;
	t=1792538999; h=From:To:Subject:Date:Message-ID:MIME-Version;
	bh=vpOI+1qBW/MtfJm0O85kyO88qqkrY/yJqDCLaRNjbVw=;
	b=QGACHvRGyo4WyDIcoCu3SwNOb7Os6K1a/tGqS6bchdxzNZs22XEekAaMW3KzF9RtLg/OK5BHC346YsZPRNp/FURgSMUTLuEVY5HgtIlPS5Vo/3H9f+XjQ1hNUHlxhu+4iSbbryXkrM6iAOWS1PB3l+HJSPjONbGzp0DwTPn4zw8=
X-Alimail-AntiSpam: AC=CONTINUE;BC=0.04501025|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_alarm|0.00619225-0.000360456-0.993447;FP=3275688948779205885|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033037034184;MF=fangxy@xiaopeng.com;NM=1;PH=DS;RN=9;RT=9;SR=0;TI=SMTPD_---.jm3sHc-_1792538997;
X-Received: from localhost.localdomain(mailfrom:fangxy@xiaopeng.com fp:SMTPD_---.jm3sHc-_1792538997 cluster:ay29)
          by smtp.aliyun-inc.com;
          Wed, 21 Oct 2026 07:29:58 +0800
X-Cc: virtualization@lists.linux.dev,
	kvm@vger.kernel.org,
	netdev@vger.kernel.org,
	stable@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Weibin Liu <liuwb@xiaopeng.com>
X-Mailer: git-send-email 2.50.1
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list