From nobody Thu Jul 23 22:27:22 2026 Received: from cstnet.cn (smtp81.cstnet.cn [159.226.251.81]) (using TLSv1.2 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 508EC26980F; Mon, 22 Jun 2026 11:25:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.226.251.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782127555; cv=none; b=akr2XZOCp0EwuymFYvNpVAdCA6vkG51UBSYej0hu9zOLOWQicCKZ8WZ3bkeyff9ZpohiVFaMRGTixcVIVZqkLmNFOIeKddJL8VNomAa3kGisIhnxBxY7/4QV63CxECSSXQ7HVTwQH3E1e0qODwl8oVW01hwsvCnjBtW+tjW5CUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782127555; c=relaxed/simple; bh=O6nPlO0AkUWv1ph8JyCcJZynxM2IH4aAcISHnLkypLY=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FxNXx2oNoZ8AnlpRb+ooFVHre/b/D6dCM4rp+T1mjCHiqkhiZ7WrUEv3e5FInopGpKuLF4fMTd11Esnd40W5j4Np21Vwul1yY5t8bP/MyACVT80AtoNh2itzjx/rtcYjH62hjoyrYJN7ukEztJQ6rBl2uNFoLFK7bViwDyrso8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn; spf=pass smtp.mailfrom=iscas.ac.cn; arc=none smtp.client-ip=159.226.251.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iscas.ac.cn Received: from localhost.localdomain (unknown [117.182.74.238]) by APP-03 (Coremail) with SMTP id rQCowADH5dy4GzlqFCSGFQ--.2637S2; Mon, 22 Jun 2026 19:25:46 +0800 (CST) From: Wentao Liang To: Juergen Gross , Stefano Stabellini Cc: Oleksandr Tyshchenko , xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Wentao Liang , stable@vger.kernel.org Subject: [PATCH v2] xen/gntdev: fix error handling in ioctl Date: Mon, 22 Jun 2026 19:25:41 +0800 Message-Id: <20260622112541.38194-1-vulab@iscas.ac.cn> X-Mailer: git-send-email 2.39.5 (Apple Git-154) 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 X-CM-TRANSID: rQCowADH5dy4GzlqFCSGFQ--.2637S2 X-Coremail-Antispam: 1UD129KBjvJXoW7AF13Zw4kWry7trWfZrW7CFg_yoW8Aw4Dp3 9xKa98Ar4rX3yIq3Wqyaya9FyFg34fKFW8CFykK3Z8ZrnIv3W2vr15tFyjgr4UJws7urW5 Zr1v9FyruFW5ArJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkl14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s 0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xII jxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r12 6r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI 0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y 0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxV WUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1l IxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbSfO7UUUU U== X-CM-SenderInfo: pyxotu46lvutnvoduhdfq/1tbiBwkGA2o43crsHAAAs+ Content-Type: text/plain; charset="utf-8" When gntdev_ioctl_map_grant_ref() fails to copy the operation result back to userspace after successfully adding the mapping to the list, the error path returns -EFAULT without releasing the reference acquired by gntdev_alloc_map(). The mapping remains in priv->maps with a refcount of 1, causing a memory leak and a dangling list entry. Additionally, gntdev_add_map() may modify map->index to avoid overlap with existing mappings. Therefore, the index returned to userspace must be obtained after gntdev_add_map() completes. Fix this by holding the mutex across gntdev_add_map(), retrieving the correct index, and copy_to_user(). If copy_to_user() fails, remove the mapping from the list and release the reference while still holding the lock. Cc: stable@vger.kernel.org Fix these issues by properly handling all error cases. Fixes: 1401c00e59ea ("xen/gntdev: convert priv->lock to a mutex") Fixes: 68b025c813c2 ("xen-gntdev: Add reference counting to maps") Signed-off-by: Wentao Liang Reviewed-by: Juergen Gross --- drivers/xen/gntdev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index 61ea855c4508..1dcc4675580e 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -670,11 +670,15 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_= priv *priv, mutex_lock(&priv->lock); gntdev_add_map(priv, map); op.index =3D map->index << PAGE_SHIFT; - mutex_unlock(&priv->lock); =20 - if (copy_to_user(u, &op, sizeof(op)) !=3D 0) + if (copy_to_user(u, &op, sizeof(op)) !=3D 0) { + list_del(&map->next); + mutex_unlock(&priv->lock); + gntdev_put_map(priv, map); return -EFAULT; + } =20 + mutex_unlock(&priv->lock); return 0; } =20 --=20 2.39.5 (Apple Git-154)