Re: [PATCH] misc: fastrpc: fix use-after-free of fastrpc_user in dma-buf ops

From: Ekansh Gupta

Date: Mon Nov 30 2026 - 03:30:59 EST


On 30-11-2026 10:22, Shawn Guo wrote:
> A buffer allocated with FASTRPC_IOCTL_ALLOC_DMA_BUFF is exported as a
> dma-buf that can outlive the fastrpc file it was allocated from, but
> it holds no reference on that file's fastrpc_user. The dma-buf
> release, attach and mmap ops nevertheless dereference buf->fl->cctx
> to strip the SID bits from the DSP address. If the fastrpc fd is
> closed first, e.g. when the remoteproc is stopped, the dma-buf release
> reads freed memory:
>
> Unable to handle kernel NULL pointer dereference at virtual address 00000000000005e9
> ...
> Workqueue: events delayed_fput
> pc : fastrpc_release+0x34/0x70 [fastrpc]
> lr : dma_buf_release+0x40/0xa0
> Call trace:
> fastrpc_release+0x34/0x70 [fastrpc] (P)
> dma_buf_release+0x40/0xa0
> dentry_kill+0xec/0x2d4
> finish_dput+0x1c/0x90
> dput+0x3c/0x40
> __fput+0x110/0x2f0
> delayed_fput+0x4c/0x68
>
> Taking a reference on fastrpc_user from the dma-buf would create a
> cycle, as fl->maps can hold the same dma-buf and is only released when
> fastrpc_user is freed. Instead, save the handle returned by
> dma_alloc_coherent() in the buffer and use it in the DMA API calls, so
> that the dma-buf ops no longer need fastrpc_user. Remove
> fastrpc_ipa_to_dma_addr(), which has no users left.
>
> The crash was seen once on Nord Ride when stopping the ADSP remoteproc,
> among many test cycles. Whether it happens depends on the dma-buf being
> released after the fastrpc fd and on the freed fastrpc_user having been
> reused.
>
> Fixes: 1d94ce8996d7 ("misc: fastrpc: Add support for new DSP IOVA formatting")
> Cc: stable@xxxxxxxxxxxxxxx # v7.0+
> Signed-off-by: Shawn Guo <shengchao.guo@xxxxxxxxxxxxxxxx>
> ---
> drivers/misc/fastrpc.c | 21 +++++++--------------
> 1 file changed, 7 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index be183e55faa1..67a16b2368ba 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -230,6 +230,8 @@ struct fastrpc_buf {
> struct device *dev;
> void *virt;
> dma_addr_t dma_addr;
> + /* Handle from dma_alloc_coherent(), without the SID bits */
> + dma_addr_t dma_handle;
Nit: can you add comment for dma_addr also? Something like:
/* DSP-visible IOVA, may include SID bits */

Reviewed-by: Ekansh Gupta <ekansh.gupta@xxxxxxxxxxxxxxxx>

> u64 size;
> /* Lock for dma buf attachments */
> struct mutex lock;
> @@ -371,14 +373,6 @@ struct fastrpc_user {
> struct kref refcount;
> };
>
> -/* Extract SMMU PA from consolidated IOVA */
> -static inline dma_addr_t fastrpc_ipa_to_dma_addr(struct fastrpc_channel_ctx *cctx, dma_addr_t iova)
> -{
> - if (!cctx->soc_data->sid_pos)
> - return 0;
> - return iova & GENMASK_ULL(cctx->soc_data->sid_pos - 1, 0);
> -}
> -
> /*
> * Prepare the consolidated iova to send to DSP by prepending the SID
> * to smmu PA at the appropriate position
> @@ -482,8 +476,7 @@ static void fastrpc_buf_free(struct fastrpc_buf *buf)
> if (!buf)
> return;
>
> - dma_free_coherent(buf->dev, buf->size, buf->virt,
> - fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr));
> + dma_free_coherent(buf->dev, buf->size, buf->virt, buf->dma_handle);
> kfree(buf);
> }
>
> @@ -515,6 +508,7 @@ static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
> return -ENOMEM;
> }
>
> + buf->dma_handle = buf->dma_addr;
> *obuf = buf;
>
> return 0;
> @@ -896,8 +890,7 @@ static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
> return -ENOMEM;
>
> ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
> - fastrpc_ipa_to_dma_addr(buffer->fl->cctx, buffer->dma_addr),
> - buffer->size);
> + buffer->dma_handle, buffer->size);
> if (ret < 0) {
> dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
> kfree(a);
> @@ -945,8 +938,8 @@ static int fastrpc_mmap(struct dma_buf *dmabuf,
>
> dma_resv_assert_held(dmabuf->resv);
>
> - return dma_mmap_coherent(buf->dev, vma, buf->virt,
> - fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr), size);
> + return dma_mmap_coherent(buf->dev, vma, buf->virt, buf->dma_handle,
> + size);
> }
>
> static const struct dma_buf_ops fastrpc_dma_buf_ops = {