From: Juergen Gross <jgross@suse.com>
To: WenTao Liang <vulab@iscas.ac.cn>, sstabellini@kernel.org
Cc: oleksandr_tyshchenko@epam.com, xen-devel@lists.xenproject.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] xen/gntdev: fix refcount leak in gntdev_ioctl_map_grant_ref()
Date: Mon, 15 Jun 2026 17:23:48 +0200 [thread overview]
Message-ID: <69464e3f-895f-4f8f-bd10-a97938e71dbe@suse.com> (raw)
In-Reply-To: <20260611142328.87566-1-vulab@iscas.ac.cn>
[-- Attachment #1.1.1: Type: text/plain, Size: 1696 bytes --]
On 11.06.26 16:23, WenTao Liang wrote:
> 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.
>
> Fix this by moving the copy_to_user() before gntdev_add_map(),
> so that the mapping is only inserted into the list on success.
> This avoids the need to remove the mapping from the list on error.
>
> Cc: stable@vger.kernel.org
> Fixes: 68b025c813c2 ("xen-gntdev: Add reference counting to maps")
> Signed-off-by: WenTao Liang <vulab@iscas.ac.cn>
> ---
> drivers/xen/gntdev.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
> index 61ea855c4508..a1c230756b3d 100644
> --- a/drivers/xen/gntdev.c
> +++ b/drivers/xen/gntdev.c
> @@ -672,8 +672,13 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
> 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) {
> + mutex_lock(&priv->lock);
> + list_del(&map->next);
> + mutex_unlock(&priv->lock);
I don't think this is race free.
Just dereferencing map without verifying it is still on the list (the mutex was
dropped in between!) might access an already freed object.
I think you need to keep the mutex held across the copy_to_user() and drop it
only after the last time accessing map.
Juergen
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
prev parent reply other threads:[~2026-06-15 15:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-11 14:23 WenTao Liang
2026-06-15 15:23 ` Juergen Gross [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=69464e3f-895f-4f8f-bd10-a97938e71dbe@suse.com \
--to=jgross@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=oleksandr_tyshchenko@epam.com \
--cc=sstabellini@kernel.org \
--cc=stable@vger.kernel.org \
--cc=vulab@iscas.ac.cn \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®