mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gray David <3059305632@qq.com>
To: Srinivas Kandagatla <srini@kernel.org>,
	 Ekansh Gupta <ekansh.gupta@oss.qualcomm.com>,
	Arnd Bergmann <arnd@arndb.de>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 stable@vger.kernel.org, Gray David <3059305632@qq.com>
Subject: [PATCH] misc: fastrpc: hold fastrpc_user reference for exported dma_buf
Date: Sat, 05 Dec 2026 17:58:10 +0800	[thread overview]
Message-ID: <tencent_4FBD978AB910B102E8FBA9CEAA49EFE25E09@qq.com> (raw)

Buffers exported via FASTRPC_IOCTL_ALLOC_DMA_BUFF (fastrpc_dmabuf_alloc())
keep a pointer to struct fastrpc_user in buf->fl. Both
fastrpc_dma_buf_attach() and fastrpc_release() -> fastrpc_buf_free()
dereference buf->fl->cctx when translating the buffer address via
fastrpc_ipa_to_dma_addr().

However, fastrpc_dmabuf_alloc() does not take a kref reference on fl.
Because the exported dma_buf has an independent lifetime from the FastRPC
device fd, closing /dev/fastrpc-* while the dma_buf fd is still open (or
exiting the process, which closes the lower-numbered device fd first)
causes fastrpc_device_release() to drop the last reference and free fl
via fastrpc_user_free(). Any subsequent fastrpc_dma_buf_attach() or
close(dmabuf_fd) -> fastrpc_release() -> fastrpc_buf_free() then
dereferences the freed fl, triggering a use-after-free / NULL pointer
dereference Oops. Even closing the dma_buf fd immediately before closing
the device fd hits the same bug because fput() defers fastrpc_release()
to the delayed_fput workqueue.

Take a reference with fastrpc_user_get(fl) once dma_buf_export() succeeds,
and release it with fastrpc_user_put(fl) in fastrpc_release() after
fastrpc_buf_free() returns. Error paths after dma_buf_export() call
dma_buf_put(), which invokes fastrpc_release() and drops the reference.
Internal buffers (fl->init_mem and fl->mmaps) are not exported as dma_bufs
and remain freed inside fastrpc_user_free(), avoiding reference cycles.

Analysis and patch drafting were assisted by AI; all changes were
reviewed by the author.

Fixes: 1d94ce8996d7 ("misc: fastrpc: Add support for new DSP IOVA formatting")
Cc: stable@vger.kernel.org
Signed-off-by: Gray David <3059305632@qq.com>
---
 drivers/misc/fastrpc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index ca5af64f3..93a358fcc 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -865,8 +865,10 @@ static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
 static void fastrpc_release(struct dma_buf *dmabuf)
 {
 	struct fastrpc_buf *buffer = dmabuf->priv;
+	struct fastrpc_user *fl = buffer->fl;
 
 	fastrpc_buf_free(buffer);
+	fastrpc_user_put(fl);
 }
 
 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
@@ -1931,6 +1933,9 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
 		return err;
 	}
 
+	/* Keep fl alive until fastrpc_release() finishes freeing buffer */
+	fastrpc_user_get(fl);
+
 	bp.fd = get_unused_fd_flags(O_ACCMODE);
 	if (bp.fd < 0) {
 		dma_buf_put(buf->dmabuf);

---
base-commit: 62225881dad6100844bd7ef1b89e30fac9b313c6
change-id: 20261205-fastrpc-dmabuf-uaf-fix-e9fbbd883ee3

Best regards,
--  
Gray David <3059305632@qq.com>


                 reply	other threads:[~2026-12-05  9:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=tencent_4FBD978AB910B102E8FBA9CEAA49EFE25E09@qq.com \
    --to=3059305632@qq.com \
    --cc=arnd@arndb.de \
    --cc=ekansh.gupta@oss.qualcomm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=srini@kernel.org \
    --cc=stable@vger.kernel.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®