[PATCH] misc: fastrpc: hold fastrpc_user reference for exported dma_buf
From: Gray David
Date: Sat Dec 05 2026 - 04:58:31 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Gray David <3059305632@xxxxxx>
---
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@xxxxxx>