From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 BBE85446839; Tue, 21 Jul 2026 19:57:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784663852; cv=none; b=CsaCMkauExByT6kEXZqziDbxGSZjUhGa/bozq6Bw6TZ24Lq92B0CspUqTVzHJybU8cBlbM3hMVv4+aNafncpY6P8VOuuRCoElKl6bvcTVubMvIV4rFDftWZLK7TZV9Gjvy9vFbxQdciSgHoNni9vCn6+/0ENmVB0QFO+6wlN0gY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784663852; c=relaxed/simple; bh=rIvzrNlzjPcmvpEW8sbnAODLTKVFE7FISd3BPd0tX4k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uy+KDUDpTMcyPr3qNPHQif1i0VjnjLKxCj0DssaKh1aiUmNudAogT1XZU+sGTxN0SSDFHroa6xmhjEFYOIh6uQawuhzWTGORIkQ61OaUEes7wP79RIxDJNVThdTF2ZbEyEcft0lXt9aCevR/ptYgH0fFLl/McPt4RIIPl8hhwZ0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=OPXhtMsL; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="OPXhtMsL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1888C1F00A3D; Tue, 21 Jul 2026 19:57:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784663850; bh=AxEY78BGqvqjvdLG8hALKG4nLu0OX1GaEsXbiRXjE5A=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OPXhtMsLMbLWNJ6+A5qWfGvffuQknHtg97D7iCihB0YOAwSCQVLYbrZoJF2Q4rtAC Lxv/alPqlCIAgHis4L8inWBXbv20bgPgm6H9n0FruxMoe983MooxyQe8zWaKbArdJA C1mH41jiQjNevAS8E1tMl1xZvtR/Rms33zqEW1t0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Wentao Liang , Juergen Gross Subject: [PATCH 6.12 0980/1276] xen/gntdev: fix error handling in ioctl Date: Tue, 21 Jul 2026 17:23:43 +0200 Message-ID: <20260721152507.949413757@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152446.065700225@linuxfoundation.org> References: <20260721152446.065700225@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Wentao Liang commit 45ca1afe2fd14c04e37227e79d3f8455831d8408 upstream. 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 Signed-off-by: Juergen Gross Message-ID: <20260622112541.38194-1-vulab@iscas.ac.cn> Signed-off-by: Greg Kroah-Hartman --- drivers/xen/gntdev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -678,11 +678,15 @@ static long gntdev_ioctl_map_grant_ref(s mutex_lock(&priv->lock); gntdev_add_map(priv, map); op.index = map->index << PAGE_SHIFT; - mutex_unlock(&priv->lock); - if (copy_to_user(u, &op, sizeof(op)) != 0) + if (copy_to_user(u, &op, sizeof(op)) != 0) { + list_del(&map->next); + mutex_unlock(&priv->lock); + gntdev_put_map(priv, map); return -EFAULT; + } + mutex_unlock(&priv->lock); return 0; }