From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F5CD7E0FF for ; Mon, 10 Aug 2026 01:03:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786323808; cv=none; b=dfbjMHWyCbS29QL+n9qxjyfXEcAh/y5iXVAuzvJth9GqAZAhdd/NJpLkz2jidDtkHESOEL6w/e9znW0+h5h6my3BUaA5kovW5tV4VhcINZhrKzf11jLh4ogYl6rEY1lSF6npk0ElyQLdr/Wa2oyxnaAMiT/Xb2A4GsTS0PaBp6Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786323808; c=relaxed/simple; bh=/UNLEhTBAnch36SgaK4rfVGlQTG/rEakKekLFDXlBQc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=upk4C7vAAWZw0VWJDLwbUzFwO4PoActwh5nyOJsbppo5aIMZrDoqC8NwoTtlROOUiVWK+QSJ9OWxHFfDJzlj9Cny9bxGUmwqAg8X/jG/O4WMYr6USkUu6nosDJSNZVrMm7Zq8fKTmEFhIwXSgr4tvs7O6wshIZMwKyT/Nq6doOE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kGwPP3m+; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kGwPP3m+" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-38deea72eebso1251066a91.1 for ; Sun, 09 Aug 2026 18:03:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786323807; x=1786928607; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=MbbU/F/djf22NMRk+tFA6K7hMv5blZDiEPyCufD11oE=; b=kGwPP3m+84MyCsrHaaTZx34HjnPtRt5oHJl2VtkddFBH/Q4o5kj7S3V7u3ytKySR86 zREW5/Lt7AoMcqgNRktRp83vbQ2z7/Bv9qOjzgVPaFerPzm93v7zyQnpT9Dmte740Aiu 7x7y4wJHn/T64staKBqy9Mssud5PqLcWQa2FEisIPNJh6+ft8/UpnONDdTtOqoIfwN6z vQNgzjKpVdOZDLVtnmcyi3uHKVp7jCer8bPFQH98gZXscyUG1kcLmLYmX17E4HhPulyF qc8ntXQlposFtb+/4LzSifw8hOVJ1IUa4lkiUOFVOcm8OB5ZRj7FasDjckeVmnui2md6 wuzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786323807; x=1786928607; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MbbU/F/djf22NMRk+tFA6K7hMv5blZDiEPyCufD11oE=; b=MHerzpQ8S9XzAoeBEcB0/R7q91IVmJJsWggguXjhtevoPzfFENOwTKaNGY5PPPYRXb aPkTJ9rYYWiMgI5KBWXTu3Q+B6NhX+BCI9TWKCTKMMbqkIMVkBeuc31Q+CeuP11ViGIO w64HiPkaJscjymdmdtfcGhAXXHYblk7NkYjzMiq5TNYlAZtLlBp0g9qopBpzZvC1VyKK bjC99c6HXujk7FVZCPaDwLRlJa0yU8RDIpnCakPL9QNm6s1kFrceWwYv+2dQbAe8H9io j9ecxD435VkzwCBFR2ENvkNrgfQicXJYb1qPKFf8ReGtdCT7nc87+1KU5dDyR9apoLgO EF0w== X-Forwarded-Encrypted: i=1; AHgh+Ro7hRr6ZRgX15MMba3qpc3xcSe+eaLOqTL3RpZGZRK/y4983kHLlhKpf8MowJXyeYlDZs7RyXuearDITIo=@vger.kernel.org X-Gm-Message-State: AOJu0YxnT6flbMAOb+VFEOGWKtpmq0Uo78dlztf2pa6GGV/iudw4g33q mKNijB8jbBB3uJam5CndEnT8gME0vYpZaBYxpiMQ4a48DSNRfPx1q1+xRwR/rg== X-Gm-Gg: AR+sD13aNIs5wCiYKeY3yAIGdbp1HsripAegfjMQWpCwPX9Dh4Fp615BEjV2uGfj/SD HDAeDLpRpaUXA1JJMYyX2nFZSUb9FasWzW6t5/d2cwQpejRxcEqnn+PDWH4R2eyXHbi1hEcPeKm 0jf+HjlIqu62D12qzH9p2GJgZu6wgI7jvo+8UPvPKDsbIZN9ZKW5iJNINrIlsbtYY7ePFwUzFV3 uRrqpr8KHX+J3M+JSPw60L8deVlUE+oC/T/feYLy8mN3Tm3aFY1TSGUY94YaLm5Nbza5wKq7siZ YbYwymTMy5eQulRKFuZmQNZnDZefAbkQw2M4nHGyRIeF1+PIfVcDDZh8+FNrXXk0Ec/ujWWTkv6 M+UVnv2UpMG5RjEV3c3QSn0VPL3BNv4BgNaawSEmMSjhLK43vbIpYzGqbJe3pBRzc1NgJaWz+Uj squghC6x5kqTGm7YZCtCXRsThowPyDS0GVj9c2ibfJ9bbd1K5b/qMIdMoe3MI= X-Received: by 2002:a17:90b:4a0f:b0:38e:9045:bac0 with SMTP id 98e67ed59e1d1-392823340e5mr16894066a91.5.1786323806331; Sun, 09 Aug 2026 18:03:26 -0700 (PDT) Received: from gmail.com ([123.118.75.63]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3925ff64b1dsm10138275a91.13.2026.08.09.18.03.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Aug 2026 18:03:25 -0700 (PDT) From: Jia Jia To: mst@redhat.com, jasowang@redhat.com Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] vhost/vdpa: reject VRING_NUM larger than device max Date: Mon, 10 Aug 2026 09:03:00 +0800 Message-Id: <20260810010300.132959-1-physicalmtea@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit vhost_vring_set_num() accepts any non-zero power-of-two queue size that fits in 16 bits. vhost-vdpa then passes that value to set_vq_num() without comparing it with get_vq_num_max(). A process with access to /dev/vhost-vdpa-* can therefore configure a queue larger than the device advertises. With vdpa_sim, the worker can walk descriptors beyond the mapped descriptor ring. KASAN reports a 16-byte out-of-bounds read, corresponding to one vring_desc, in the vringh IOTLB path: BUG: KASAN: out-of-bounds in _copy_from_iter Read of size 16 copy_from_iotlb copydesc_iotlb vringh_getdesc_iotlb vdpasim_net_work Cache get_vq_num_max() immediately after reset. Some backends derive it from writable queue-size state, so querying it after SET_NUM may return the current size instead of the device capability. Invalidate the cached value before reset so a failed reset leaves SET_NUM disabled. For VHOST_SET_VRING_NUM, copy the complete vring state once and use the same index and size for validation, vq->num, and set_vq_num(). This ensures that validation and use operate on the same copied values. Fixes: 4c8cf31885f6 ("vhost: introduce vDPA-based backend") Signed-off-by: Jia Jia --- v3: - Copy VHOST_SET_VRING_NUM once and use the copied values throughout, avoiding the double-fetch race in v2. - Cache the device-wide maximum after reset so mutable queue state cannot break a later legal resize; fail closed if reset fails. v2: - Add the missing Signed-off-by tag. drivers/vhost/vdpa.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 692564b1bcbb..f2eedb0464c2 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -53,6 +53,7 @@ struct vhost_vdpa { struct cdev cdev; atomic_t opened; u32 nvqs; + u16 vq_num_max; int virtio_id; int minor; struct eventfd_ctx *config_ctx; @@ -229,7 +230,9 @@ static void vhost_vdpa_unsetup_vq_irq(struct vhost_vdpa *v, u16 qid) static int _compat_vdpa_reset(struct vhost_vdpa *v) { struct vdpa_device *vdpa = v->vdpa; + const struct vdpa_config_ops *ops = vdpa->config; u32 flags = 0; + int ret; v->suspended = false; @@ -239,7 +242,14 @@ static int _compat_vdpa_reset(struct vhost_vdpa *v) VDPA_RESET_F_CLEAN_MAP : 0; } - return vdpa_reset(vdpa, flags); + v->vq_num_max = 0; + ret = vdpa_reset(vdpa, flags); + if (!ret) { + /* Some backends derive the max from mutable queue state. */ + v->vq_num_max = ops->get_vq_num_max(vdpa); + } + + return ret; } static int vhost_vdpa_reset(struct vhost_vdpa *v) @@ -641,9 +651,15 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, u32 idx; long r; - r = get_user(idx, (u32 __user *)argp); - if (r < 0) - return r; + if (cmd == VHOST_SET_VRING_NUM) { + if (copy_from_user(&s, argp, sizeof(s))) + return -EFAULT; + idx = s.index; + } else { + r = get_user(idx, (u32 __user *)argp); + if (r < 0) + return r; + } if (idx >= v->nvqs) return -ENOBUFS; @@ -652,6 +668,23 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, vq = &v->vqs[idx]; switch (cmd) { + case VHOST_SET_VRING_NUM: + mutex_lock(&vq->mutex); + if (vq->private_data) { + r = -EBUSY; + } else if (!s.num || s.num > 0xffff || + s.num > v->vq_num_max || + (s.num & (s.num - 1))) { + r = -EINVAL; + } else { + vq->num = s.num; + r = 0; + } + mutex_unlock(&vq->mutex); + if (r) + return r; + ops->set_vq_num(vdpa, idx, s.num); + return 0; case VHOST_VDPA_SET_VRING_ENABLE: if (copy_from_user(&s, argp, sizeof(s))) return -EFAULT; @@ -765,9 +798,6 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, ops->set_vq_cb(vdpa, idx, &cb); break; - case VHOST_SET_VRING_NUM: - ops->set_vq_num(vdpa, idx, vq->num); - break; } return r; -- 2.34.1