misc: fastrpc: fix use-after-free on map->fl in device release
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.
--- drivers/misc/fastrpc.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/misc/fastrpc.c +5 -2
@@ -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)
Srinivas Kandagatla
maintainerQuoted 7 lines · Zhenghang Xiao
fastrpc_device_release()callsfastrpc_map_put()for each map without clearingmap->fl, then frees thefastrpc_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()dereferencesmap->fl->lock-- a use-after-free.
Quoted 5 lines
Detach maps from fl and clear
map->flin the release path before freeing fl. Also guard theFASTRPC_ATTR_SECUREMAPsection infastrpc_free_map()against NULLmap->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")Signed-off-by Zhenghang Xiao <[email protected]>--- drivers/misc/fastrpc.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/misc/fastrpc.c +5
-2@@ -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);