Skip to contentSkip to conversation

rapidio: mport_cdev: fix sequential UAF in dma_req_free()

PATCH In review James Kim 1 participant 2 messages Apr 6 – Apr 21

Subsystems (2)
lore
Share & tools
lore

James Kim

author
#0 lore raw

PATCH 1/1 rapidio: mport_cdev: fix sequential UAF 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 sequential (non-racy) 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")
---
 drivers/rapidio/devices/rio_mport_cdev.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Resending this patch as it might have been missed due to the merge window.

No changes since the previous submission.

drivers/rapidio/devices/rio_mport_cdev.c +8 -3
index 7df466e22282..5fb6ec439028 100644
@@ -582,9 +582,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.25.1
author
#1 lore raw

Hi,

Just a gentle reminder about this patch.

It might have been missed during the previous merge window.

Thanks,
James

Quoted 47 lines · James Kim

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 sequential (non-racy) 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")
---
 drivers/rapidio/devices/rio_mport_cdev.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Resending this patch as it might have been missed due to the merge window.

No changes since the previous submission.

drivers/rapidio/devices/rio_mport_cdev.c +8 -4
index 7df466e22282..5fb6ec439028 100644
@@ -582,9 +582,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.25.1