mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ekansh Gupta <ekansh.gupta@oss.qualcomm.com>
To: Shawn Guo <shengchao.guo@oss.qualcomm.com>,
	Srinivas Kandagatla <srini@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Kumari Pallavi <kumari.pallavi@oss.qualcomm.com>,
	Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>,
	linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] misc: fastrpc: fix use-after-free of fastrpc_user in dma-buf ops
Date: Mon, 30 Nov 2026 14:00:46 +0530	[thread overview]
Message-ID: <6f4a51b1-a9be-40a5-9961-da2a786656e4@oss.qualcomm.com> (raw)
In-Reply-To: <20261130045227.4150363-1-shengchao.guo@oss.qualcomm.com>

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@vger.kernel.org # v7.0+
> Signed-off-by: Shawn Guo <shengchao.guo@oss.qualcomm.com>
> ---
>  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@oss.qualcomm.com>

>  	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 = {


      reply	other threads:[~2026-11-30  8:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  4:52 Shawn Guo
2026-11-30  8:30 ` Ekansh Gupta [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=6f4a51b1-a9be-40a5-9961-da2a786656e4@oss.qualcomm.com \
    --to=ekansh.gupta@oss.qualcomm.com \
    --cc=arnd@arndb.de \
    --cc=dmitry.baryshkov@oss.qualcomm.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=kumari.pallavi@oss.qualcomm.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shengchao.guo@oss.qualcomm.com \
    --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®