2021-03-23 02:08:08

by Lv Yunlong

[permalink] [raw]
Subject: [PATCH] rapidio: Fix a use after free in mport_mm_close

In mport_mm_close, map is freed by kref_put(&map->ref,).
But mutex_unlock(&map->md->buf_mutex) uses map later.
Other thread could allocate the freed chunk and cause
an unexpected result.

My patch adds a variable buf_mutex to store the mutex before
map is freed.

Signed-off-by: Lv Yunlong <[email protected]>
---
drivers/rapidio/devices/rio_mport_cdev.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/rapidio/devices/rio_mport_cdev.c b/drivers/rapidio/devices/rio_mport_cdev.c
index 94331d999d27..836bcb4ab50a 100644
--- a/drivers/rapidio/devices/rio_mport_cdev.c
+++ b/drivers/rapidio/devices/rio_mport_cdev.c
@@ -2179,9 +2179,11 @@ static void mport_mm_close(struct vm_area_struct *vma)
struct rio_mport_mapping *map = vma->vm_private_data;

rmcd_debug(MMAP, "%pad", &map->phys_addr);
- mutex_lock(&map->md->buf_mutex);
+ struct mutex buf_mutex = map->md->buf_mutex;
+
+ mutex_lock(&buf_mutex);
kref_put(&map->ref, mport_release_mapping);
- mutex_unlock(&map->md->buf_mutex);
+ mutex_unlock(&buf_mutex);
}

static const struct vm_operations_struct vm_ops = {
--
2.25.1