Skip to contentSkip to conversation

misc: fastrpc: remove mmap entry before remote unmap

PATCH In review Yousef Alhouseen 3 participants 3 messages Jun 24 – Jul 31

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fastrpc: remove mmap entry before remote unmap

FASTRPC_IOCTL_MUNMAP looked up a buffer and dropped fl->lock.

It only deleted the list entry after the remote unmap succeeded.

Two threads can therefore find the same entry and unmap it twice.

That can lead to a double list deletion and double free.

Detach the entry before invoking the DSP.

Put it back if the remote unmap fails.

---
 drivers/misc/fastrpc.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/fastrpc.c +12 -2
index ba4ade874..6a5239b1c 100644
@@ -1902,7 +1902,8 @@ static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *
 	if (!err) {
 		dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
 		spin_lock(&fl->lock);
-		list_del(&buf->node);
+		if (!list_empty(&buf->node))
+			list_del_init(&buf->node);
 		spin_unlock(&fl->lock);
 		fastrpc_buf_free(buf);
 	} else {
@@ -1917,6 +1918,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 	struct fastrpc_buf *buf = NULL, *iter, *b;
 	struct fastrpc_req_munmap req;
 	struct device *dev = fl->sctx->dev;
+	int err;
 
 	if (copy_from_user(&req, argp, sizeof(req)))
 		return -EFAULT;
@@ -1925,6 +1927,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 	list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
 		if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
 			buf = iter;
+			list_del_init(&buf->node);
 			break;
 		}
 	}
@@ -1936,7 +1939,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 		return -EINVAL;
 	}
 
-	return fastrpc_req_munmap_impl(fl, buf);
+	err = fastrpc_req_munmap_impl(fl, buf);
+	if (err) {
+		spin_lock(&fl->lock);
+		list_add_tail(&buf->node, &fl->mmaps);
+		spin_unlock(&fl->lock);
+	}
+
+	return err;
 }
 
 static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Use-After-Free and list corruption on the fastrpc_req_munmap error path due to unhandled race with fastrpc_req_mmap.

Pre-existing issues:
- [Critical] Missing TrustZone memory cleanup leads to a fatal hypervisor Denial of Service upon reallocation.
- [High] Concurrent double-unmap vulnerability leading to Use-After-Free in fastrpc_req_mem_unmap_impl. --

commit ddfb73eb827659f1e8a6e70335752c48fead38e3
Author: Yousef Alhouseen <[email protected]>

misc: fastrpc: remove mmap entry before remote unmap

This patch removes the buffer from the fastrpc user mmaps list before calling into the DSP to unmap memory. If the remote unmap fails, the buffer is added back to the list.

Quoted 4 lines
drivers/misc/fastrpc.c +0 -0
index f3a49384586d1..964d3ab845f87 100644

[ ... ]

drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1933

Quoted 9 lines
drivers/misc/fastrpc.c +4 -1
@@ -1924,7 +1927,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 		return -EINVAL;
 	}
 
-	return fastrpc_req_munmap_impl(fl, buf);
+	err = fastrpc_req_munmap_impl(fl, buf);
+	if (err) {
+		spin_lock(&fl->lock);
+		list_add_tail(&buf->node, &fl->mmaps);

[Severity: High]
Can this fallback path corrupt the list?

If a concurrent thread calling fastrpc_req_mmap() fails in copy_to_user(), it branches to err_assign and calls fastrpc_req_munmap_impl() while the buffer is still attached to the fl->mmaps list.

If another thread concurrently calls fastrpc_req_munmap() on the same buffer, it detaches it from the list and calls fastrpc_req_munmap_impl(). If the first thread succeeds in the DSP unmap and frees the buffer, and the second thread fails the DSP unmap, it will execute list_add_tail() on the already-freed buffer.

drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1950

Quoted 5 lines
drivers/misc/fastrpc.c +4 -0
+		spin_unlock(&fl->lock);
+	}
+
+	return err;
 }

[Severity: High]
This isn't a bug introduced by this patch, but the exact same concurrent vulnerability you fixed in fastrpc_req_munmap() remains unaddressed in fastrpc_req_mem_unmap_impl().

drivers/misc/fastrpc.c:fastrpc_req_mem_unmap_impl() {
    ...
	spin_lock(&fl->lock);
	list_for_each_entry_safe(iter, m, &fl->maps, node) {
		if ((req->fd < 0 || iter->fd == req->fd) &&
		    (iter->raddr == req->vaddr)) {
			map = iter;
			break;
		}
	}
	spin_unlock(&fl->lock);
    ...
	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
				      &args[0]);
	if (err) {
		dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n",
			map->fd, map->raddr);
		return err;
	}
	fastrpc_map_put(map);
    ...
}

Could two threads calling FASTRPC_IOCTL_MEM_UNMAP on the same mapping both find the map in fl->maps because the lock is dropped without detaching the map or taking a reference?

If the first thread succeeds and frees the map, would the second thread trigger a use-after-free when it logs the error, or a double-free if it succeeds?

[Severity: Critical]
This is a pre-existing issue, but does fastrpc_buf_free() fail to restore High-Level OS (HLOS) permissions before releasing memory?

When allocating memory with the ADSP_MMAP_REMOTE_HEAP_ADDR flag:

drivers/misc/fastrpc.c:fastrpc_req_mmap() {
    ...
	if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) {
		u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);

		err = qcom_scm_assign_mem(buf->dma_addr, (u64)buf->size,
			&src_perms, fl->cctx->vmperms, fl->cctx->vmcount);
    ...
}

This transfers physical memory ownership from HLOS to TrustZone. However, when fastrpc_buf_free() is called:

drivers/misc/fastrpc.c:fastrpc_buf_free() {
	dma_free_coherent(buf->dev, buf->size, buf->virt,
			  fastrpc_ipa_to_dma_addr(buf->fl->cctx,
						  buf->dma_addr));
	kfree(buf);
}

The memory is returned to the kernel allocator without reverting the TrustZone assignment. Might a subsequent kernel allocation that touches this physical memory trigger a fatal hypervisor exception and system crash?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#2 lore raw
In reply to Yousef Alhouseen
Quoted 4 lines · Yousef Alhouseen

FASTRPC_IOCTL_MUNMAP looked up a buffer and dropped fl->lock.

It only deleted the list entry after the remote unmap succeeded.

Quoted 8 lines

Two threads can therefore find the same entry and unmap it twice.

That can lead to a double list deletion and double free.

Detach the entry before invoking the DSP.

Put it back if the remote unmap fails.

There was a fix that got recently merged to fix the same problem[1].

Please check it and let me know if your change still holds.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/drivers/misc/fastrpc.c?id=902b8ec9dd1098cc133cffa8be0b6378544f27ba

//ekansh> Signed-off-by: Yousef Alhouseen <[email protected]>

Quoted 50 lines
---
 drivers/misc/fastrpc.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/fastrpc.c +12 -2
index ba4ade874..6a5239b1c 100644
@@ -1902,7 +1902,8 @@ static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *
 	if (!err) {
 		dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
 		spin_lock(&fl->lock);
-		list_del(&buf->node);
+		if (!list_empty(&buf->node))
+			list_del_init(&buf->node);
 		spin_unlock(&fl->lock);
 		fastrpc_buf_free(buf);
 	} else {
@@ -1917,6 +1918,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 	struct fastrpc_buf *buf = NULL, *iter, *b;
 	struct fastrpc_req_munmap req;
 	struct device *dev = fl->sctx->dev;
+	int err;
 
 	if (copy_from_user(&req, argp, sizeof(req)))
 		return -EFAULT;
@@ -1925,6 +1927,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 	list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
 		if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
 			buf = iter;
+			list_del_init(&buf->node);
 			break;
 		}
 	}
@@ -1936,7 +1939,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
 		return -EINVAL;
 	}
 
-	return fastrpc_req_munmap_impl(fl, buf);
+	err = fastrpc_req_munmap_impl(fl, buf);
+	if (err) {
+		spin_lock(&fl->lock);
+		list_add_tail(&buf->node, &fl->mmaps);
+		spin_unlock(&fl->lock);
+	}
+
+	return err;
 }
 
 static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)