All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Baineng Shou" <shoubaineng@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v3 2/2] misc: fastrpc: don't publish fd before copy_to_user() succeeds
Date: Tue, 14 Jul 2026 12:08:36 +0000	[thread overview]
Message-ID: <20260714120836.EA55A1F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260714114654.3885457-3-shoubaineng@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The `fastrpc_user` context can be used-after-free if an exported dmabuf outlives the fastrpc device file descriptor.
- [Critical] Concurrent calls to `FASTRPC_IOCTL_MUNMAP` lead to list corruption, Use-After-Free, and Double-Free of a `fastrpc_buf`.
- [Critical] Concurrent calls to `FASTRPC_IOCTL_MEM_UNMAP` cause a Use-After-Free and Double-Free of a `fastrpc_map`.
--

--- Patch [2]: [PATCH v3 2/2] misc: fastrpc: don't publish fd before copy_to_user() succeeds ---
commit 385ee7a3198521d1682f7120112f746dede7b20d
Author: Baineng Shou <shoubaineng@gmail.com>
misc: fastrpc: don't publish fd before copy_to_user() succeeds

This commit fixes an issue where the DMA buffer file descriptor is published 
to userspace before a successful usercopy. It reserves an unused fd, performs 
the copy, and only installs the fd if the copy succeeds. On failure, it 
cleanly unwinds the fd and buffer allocations.

> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index f3a49384586d1..c5143cd257674 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
[ ... ]
> @@ -1709,24 +1709,20 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
>  		return err;
>  	}
>  
> -	bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
> +	bp.fd = get_unused_fd_flags(O_ACCMODE);

[Severity: Critical]
This is a pre-existing issue, but does fastrpc_buf_alloc() fail to take a 
reference on the fastrpc_user context?

Looking just above this diff in fastrpc_dmabuf_alloc():

err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);

This allocates the buffer and assigns the user context to buf->fl, but does 
not increment the user context's reference count. If the fastrpc device file 
descriptor is closed, fastrpc_device_release() drops the reference to fl and 
frees it, meaning the exported dmabuf can outlive it.

If the dmabuf is closed later, fastrpc_release() calls fastrpc_buf_free(), 
which dereferences the freed context here:

fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr)

Can this lead to a use-after-free of the fastrpc_user context?

[Severity: Critical]
This is also a pre-existing issue, but does concurrent execution of 
FASTRPC_IOCTL_MUNMAP lead to list corruption or double-frees?

In fastrpc_req_munmap(), the buf pointer is located under the lock:

buf = iter;
break;

The lock is then dropped without taking a reference count on the buffer. The 
raw pointer is passed to fastrpc_req_munmap_impl(). If two threads invoke 
the ioctl concurrently with the same arguments, could they both find the 
same buf and race? 

They would both concurrently execute this in fastrpc_req_munmap_impl():

list_del(&buf->node);
spin_unlock(&fl->lock);
fastrpc_buf_free(buf);

Does this result in list_del() and fastrpc_buf_free() executing twice on 
the same pointer?

[Severity: Critical]
This is another pre-existing issue, but does fastrpc_req_mem_unmap_impl() 
have a similar race condition?

The map is extracted from the list under the lock, and then the lock is 
dropped without incrementing the map's reference count:

map = iter;
break;
}
}
spin_unlock(&fl->lock);

Multiple threads could extract the same map pointer. If both threads 
successfully proceed, they will both call:

fastrpc_map_put(map);

Since fastrpc_map_get() was never called during the lookup, does the first 
thread drop the reference count to 0 and free the map prematurely? This 
could cause the second thread to trigger a use-after-free on failure paths 
or a double-free when calling fastrpc_map_put() again.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260714114654.3885457-1-shoubaineng@gmail.com?part=2

  reply	other threads:[~2026-07-14 12:08 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03  8:09 [PATCH] dma-buf: dma-heap: close installed fd when copy_to_user() fails Baineng Shou
2026-07-03  8:17 ` sashiko-bot
2026-07-03  8:26 ` Christian König
2026-07-10 10:57 ` [PATCH v2] dma-buf: dma-heap: don't publish fd before copy_to_user() succeeds Baineng Shou
2026-07-10 11:06   ` sashiko-bot
2026-07-10 20:20     ` T.J. Mercier
2026-07-11  4:18       ` 寿柏能
2026-07-13 23:33         ` T.J. Mercier
2026-07-14 11:46           ` [PATCH v3 0/2] dma-buf: fix fd leak when copy_to_user() fails after fd_install() Baineng Shou
2026-07-14 11:46             ` [PATCH v3 1/2] dma-buf: dma-heap: don't publish fd before copy_to_user() succeeds Baineng Shou
2026-07-14 13:13               ` David Laight
2026-07-14 13:38                 ` 寿柏能
2026-07-14 14:33                   ` David Laight
2026-07-14 11:46             ` [PATCH v3 2/2] misc: fastrpc: " Baineng Shou
2026-07-14 12:08               ` sashiko-bot [this message]
2026-07-14 12:24             ` [PATCH v3 0/2] dma-buf: fix fd leak when copy_to_user() fails after fd_install() Christian König
2026-07-14 13:27               ` [PATCH v3] drm/prime: use dma_buf_fd_install() to preserve export tracing Baineng Shou
2026-07-14 13:45                 ` sashiko-bot
2026-07-10 12:33   ` [PATCH v2] dma-buf: dma-heap: don't publish fd before copy_to_user() succeeds Christian König
2026-07-11  4:14 ` Baineng Shou
2026-07-11  4:29   ` sashiko-bot

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=20260714120836.EA55A1F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shoubaineng@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.