Skip to contentSkip to conversation

misc: fastrpc: fix use-after-free on map->fl in device release

PATCH In review Zhenghang Xiao 2 participants 2 messages May 26 – May 30

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

PATCH 1/1 misc: fastrpc: fix use-after-free on map->fl in device release

fastrpc_device_release() calls fastrpc_map_put() for each map without clearing map->fl, then frees the fastrpc_user (fl) structure. If a map's refcount is > 1 (held by a pending invoke context), the map survives with a dangling fl pointer. When the deferred workqueue later drops the context's last reference, fastrpc_context_free() -> fastrpc_map_put() -> fastrpc_free_map() dereferences map->fl->lock -- a use-after-free.

Detach maps from fl and clear map->fl in the release path before freeing fl. Also guard the FASTRPC_ATTR_SECUREMAP section in fastrpc_free_map() against NULL map->fl, since maps detached during release will have fl cleared.

Fixes 5bb96c8f9268 ("misc: fastrpc: Don't remove map on creater_process and device_release")
---
 drivers/misc/fastrpc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/misc/fastrpc.c +5 -2
index 1080f9acf70a..3c7aec9e938e 100644
@@ -337,7 +337,7 @@ static void fastrpc_free_map(struct kref *ref)
 	map = container_of(ref, struct fastrpc_map, refcount);
 
 	if (map->table) {
-		if (map->attr & FASTRPC_ATTR_SECUREMAP) {
+		if (map->fl && (map->attr & FASTRPC_ATTR_SECUREMAP)) {
 			struct qcom_scm_vmperm perm;
 			int vmid = map->fl->cctx->vmperms[0].vmid;
 			u64 src_perms = BIT(QCOM_SCM_VMID_HLOS) | BIT(vmid);
@@ -1598,8 +1598,11 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
 		fastrpc_context_put(ctx);
 	}
 
-	list_for_each_entry_safe(map, m, &fl->maps, node)
+	list_for_each_entry_safe(map, m, &fl->maps, node) {
+		list_del(&map->node);
+		map->fl = NULL;
 		fastrpc_map_put(map);
+	}
 
 	list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
 		list_del(&buf->node);
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Quoted 7 lines · Zhenghang Xiao

fastrpc_device_release() calls fastrpc_map_put() for each map without clearing map->fl, then frees the fastrpc_user (fl) structure. If a map's refcount is > 1 (held by a pending invoke context), the map survives with a dangling fl pointer. When the deferred workqueue later drops the context's last reference, fastrpc_context_free() -> fastrpc_map_put() -> fastrpc_free_map() dereferences map->fl->lock -- a use-after-free.

Quoted 5 lines

Detach maps from fl and clear map->fl in the release path before freeing fl. Also guard the FASTRPC_ATTR_SECUREMAP section in fastrpc_free_map() against NULL map->fl, since maps detached during release will have fl cleared.

Thanks for the patch, have you tested this on linux-next this should be fixed with
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/drivers/misc/fastrpc.c?id=b01bf21ae7e7c4c7cd4f1c8419bafc1e04c008e4
?

--srini

Quoted 32 lines
Fixes 5bb96c8f9268 ("misc: fastrpc: Don't remove map on creater_process and device_release")
---
 drivers/misc/fastrpc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/misc/fastrpc.c +5 -2
index 1080f9acf70a..3c7aec9e938e 100644
@@ -337,7 +337,7 @@ static void fastrpc_free_map(struct kref *ref)
 	map = container_of(ref, struct fastrpc_map, refcount);
 
 	if (map->table) {
-		if (map->attr & FASTRPC_ATTR_SECUREMAP) {
+		if (map->fl && (map->attr & FASTRPC_ATTR_SECUREMAP)) {
 			struct qcom_scm_vmperm perm;
 			int vmid = map->fl->cctx->vmperms[0].vmid;
 			u64 src_perms = BIT(QCOM_SCM_VMID_HLOS) | BIT(vmid);
@@ -1598,8 +1598,11 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
 		fastrpc_context_put(ctx);
 	}
 
-	list_for_each_entry_safe(map, m, &fl->maps, node)
+	list_for_each_entry_safe(map, m, &fl->maps, node) {
+		list_del(&map->node);
+		map->fl = NULL;
 		fastrpc_map_put(map);
+	}
 
 	list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
 		list_del(&buf->node);