rapidio: mport_cdev: fix use-after-free in dma_req_free()
Subsystems (2)
PATCH v2 1/1 rapidio: mport_cdev: fix use-after-free in dma_req_free()
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.
---
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(-)
drivers/rapidio/devices/rio_mport_cdev.c +8 -3
@@ -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);
Signature · 1 line
2.43.0
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
Quoted 48 lines · James Kim
dma_req_free()drops the mapping reference underbuf_mutexand then dereferencesreq->mapagain to unlock the mutex.If
kref_put()drops the last reference,mport_release_mapping()frees the mapping, and the subsequentmutex_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")Signed-off-by James Kim <[email protected]>---
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(-)drivers/rapidio/devices/rio_mport_cdev.c +8
-4@@ -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
Dan Carpenter
Reviewed-byQuoted 5 lines · James Kim
Hi,
This is a gentle ping for this patch, as it has not received any feedback for some time.
Quoted 8 lines
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
Yep. I think this is a real bug and the fix looks correct.
regards,
dan carpenter
Quoted 11 lines · On Mon, 15 Jun 2026 16:05:30 +0900 James Kim <[email protected]> wrote:
dma_req_free()drops the mapping reference underbuf_mutexand then dereferencesreq->mapagain to unlock the mutex.If
kref_put()drops the last reference,mport_release_mapping()frees the mapping, and the subsequentmutex_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")
12 characters of hash, please.
Quoted 1 line
Is this observable from userspace in any way?
Quoted 8 lines
drivers/rapidio/devices/rio_mport_cdev.c +0
-2@@ -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);
drivers/rapidio/devices/rio_mport_cdev.c static void dma_req_free(struct kref *ref) · line 575
Quoted 10 lines
drivers/rapidio/devices/rio_mport_cdev.c +8
-1-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 ; }
lgtm, although rio object lifetimes aren't exactly my strength ;)
It might be safer/saner to do the `req->map = NULL' inside the mutex?
And....
You made Sashiko AI review look at this code. The results aren't pretty:
https://sashiko.dev/#/patchset/[email protected]
It claims to have found fourteen pre-existing flaws in there :(
Quoted 1 line · On Mon, 15 Jun 2026 16:05:30 +0900 James Kim <[email protected]> wrote:
Fixes 4b0986a36 ("rapidio: add mport character device support")
I cannot find this commit. I'll use
e8de370188d0 ("rapidio: add mport char device driver")
Hi Andrew,
Thanks for the review.
Yes, this path is reachable from userspace through the RapidIO mport character-device interface. When the final mapping reference is dropped while releasing a DMA request, the subsequent mutex_unlock() may dereference freed memory, leading to a kernel crash or a KASAN report.
I'll send a v3 which:
- updates the Fixes tag to the correct commit,
- clears req->map while holding md->buf_mutex as suggested, and
- clarifies the userspace reachability in the commit message.
I'll also review the additional Sashiko findings separately rather than mixing them into this fix.
Thanks,
James
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Mon, 15 Jun 2026 16:05:30 +0900 James Kim <[email protected]> wrote:
Fixes 4b0986a36 ("rapidio: add mport character device support")I cannot find this commit. I'll use
e8de370188d0("rapidio: add mport char device driver")
Quoted 9 lines · James Kim
Hi Andrew,
Thanks for the review.
Yes, this path is reachable from userspace through the RapidIO mport character-device interface. When the final mapping reference is dropped while releasing a DMA request, the subsequent
mutex_unlock()may dereference freed memory, leading to a kernel crash or a KASAN report.
Quoted 8 lines
I'll send a v3 which:
- updates the Fixes tag to the correct commit,
- clearsreq->mapwhile holdingmd->buf_mutexas suggested, and
- clarifies the userspace reachability in the commit message.I'll also review the additional Sashiko findings separately rather than mixing them into this fix.
These kinds of use after free bugs are hard to detect at runtime unless you have poisoning enabled. It's freed and then used immediately so it would be hard for a race condition to change the memory between the free and the use.
regards,
dan carpenter
PATCH v3 1/1 rapidio: mport_cdev: fix use-after-free in dma_req_free()
In reply to James Kimdma_req_free() acquires buf_mutex through req->map, drops the mapping reference with kref_put(), 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(), clearing req->map while holding buf_mutex, and using the cached md for mutex unlocking.
The bug is reachable from userspace via the RapidIO mport character device interface.
---
Changes since v2:
- Correct the Fixes tag.
- Clear req->map while holding buf_mutex.
- Clarify that the bug is reachable from userspace.
- Update Dan Carpenter's email address and add his Reviewed-by tag.
Changes since v1:
- Rebase 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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/rapidio/devices/rio_mport_cdev.c +7 -3
@@ -564,9 +564,13 @@ 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); + req->map =NULL ; +kref_put (&map->ref, mport_release_mapping); +mutex_unlock (&md->buf_mutex); }kref_put (&priv->dma_ref, mport_release_dma);
Signature · 1 line
2.25.1
Hi Andrew,
Thanks for picking up this patch.
I noticed that the version currently queued in mm-everything appears to correspond to v2 (commit c044e529fa01).
The v3 update includes one additional change based on your review by clearing req->map while holding md->buf_mutex:
mutex_lock(&md->buf_mutex);
req->map = NULL;
kref_put(&map->ref, mport_release_mapping);
mutex_unlock(&md->buf_mutex);The v3 posting is available here:
https://lore.kernel.org/lkml/[email protected]/
If it's still convenient, could you please replace the queued version with v3?
Thanks!
James
Quoted 59 lines · James Kim
dma_req_free()acquiresbuf_mutexthroughreq->map, drops the mapping reference withkref_put(), and then dereferencesreq->mapagain to unlock the mutex.If
kref_put()drops the last reference,mport_release_mapping()frees the mapping, and the subsequentmutex_unlock()dereferences a freed object. This is a use-after-free.Fix this by caching map and md before
kref_put(), clearingreq->mapwhile holdingbuf_mutex, and using the cached md for mutex unlocking.The bug is reachable from userspace via the RapidIO mport character device interface.
Fixes e8de370188d0 ("rapidio: add mport char device driver")Reviewed-by Dan Carpenter <[email protected]>Signed-off-by James Kim <[email protected]>---
Changes since v2:
- Correct the Fixes tag.
- Clearreq->mapwhile holdingbuf_mutex.
- Clarify that the bug is reachable from userspace.
- Update Dan Carpenter's email address and add his Reviewed-by tag.Changes since v1:
- Rebase 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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)drivers/rapidio/devices/rio_mport_cdev.c +7
-4@@ -564,9 +564,13 @@ 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); + req->map =NULL ; +kref_put (&map->ref, mport_release_mapping); +mutex_unlock (&md->buf_mutex); }kref_put (&priv->dma_ref, mport_release_dma); --2.25.1