Re: [PATCH v2] rapidio: mport_cdev: fix use-after-free in dma_req_free()
From: James Kim
Date: Tue Jul 21 2026 - 20:37:01 EST
Hi,
This is a gentle ping for this patch, as it has not received any
feedback for some time.
I'm CC'ing Dan Carpenter and Andrew Morton for additional visibility,
as this patch fixes a potential use-after-free in the RapidIO driver.
Any comments or reviews would be greatly appreciated.
Thanks,
James Kim
On Mon, Jun 15, 2026 at 4:08 PM James Kim <james010kim@xxxxxxxxx> wrote:
>
> dma_req_free() drops the mapping reference under buf_mutex and then
> dereferences req->map again to unlock the mutex.
>
> If kref_put() drops the last reference, mport_release_mapping() frees
> the mapping, and the subsequent mutex_unlock() dereferences a freed
> object. This is a use-after-free.
>
> Fix this by caching map and md before kref_put() and using the cached
> md for mutex unlocking.
>
> Fixes: 4b0986a36 ("rapidio: add mport character device support")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: James Kim <james010kim@xxxxxxxxx>
> ---
> Changes since v1:
> - Rebased on v7.1.
> - Add Dan Carpenter to Cc for lifetime and use-after-free review.
> - No functional changes.
>
> drivers/rapidio/devices/rio_mport_cdev.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/rapidio/devices/rio_mport_cdev.c b/drivers/rapidio/devices/rio_mport_cdev.c
> index 009b3b595bbf..9b94c9b2fad0 100644
> --- a/drivers/rapidio/devices/rio_mport_cdev.c
> +++ b/drivers/rapidio/devices/rio_mport_cdev.c
> @@ -564,9 +564,14 @@ static void dma_req_free(struct kref *ref)
> }
>
> if (req->map) {
> - mutex_lock(&req->map->md->buf_mutex);
> - kref_put(&req->map->ref, mport_release_mapping);
> - mutex_unlock(&req->map->md->buf_mutex);
> + struct rio_mport_mapping *map = req->map;
> + struct mport_dev *md = map->md;
> +
> + mutex_lock(&md->buf_mutex);
> + kref_put(&map->ref, mport_release_mapping);
> + mutex_unlock(&md->buf_mutex);
> +
> + req->map = NULL;
> }
>
> kref_put(&priv->dma_ref, mport_release_dma);
> --
> 2.43.0
>