misc: fastrpc: fix use-after-free race in fastrpc_map_create
PATCH 1/1 misc: fastrpc: fix use-after-free race in fastrpc_map_create
fastrpc_map_lookup returns a raw pointer after releasing fl->lock. The caller fastrpc_map_create then calls fastrpc_map_get (kref_get_unless_zero) on this unprotected pointer. A concurrent MEM_UNMAP can free the map between the lock release and the kref operation, resulting in a use-after-free on the freed slab object.
Restore the take_ref parameter to fastrpc_map_lookup so the reference is acquired atomically under fl->lock before the pointer is exposed to the caller.
--- drivers/misc/fastrpc.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/misc/fastrpc.c +11 -14
@@ -386,7 +386,7 @@ static int fastrpc_map_get(struct fastrpc_map *map)static int fastrpc_map_lookup (struct fastrpc_user *fl,int fd, -struct fastrpc_map **ppmap) +struct fastrpc_map **ppmap,bool take_ref) {struct fastrpc_map *map =NULL ;struct dma_buf *buf;
@@ -401,6 +401,12 @@ static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,if (map->fd != fd || map->buf != buf)continue ; +if (take_ref) { + ret =fastrpc_map_get (map); +if (ret) +break ; + } + *ppmap = map; ret =0 ;break ;
@@ -871,19 +877,10 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,static int fastrpc_map_create (struct fastrpc_user *fl,int fd,u64 len,u32 attr,struct fastrpc_map **ppmap) { -struct fastrpc_session_ctx *sess = fl->sctx; -int err =0 ; - -if (!fastrpc_map_lookup (fl, fd, ppmap)) { -if (!fastrpc_map_get (*ppmap)) -return 0 ; -dev_dbg (sess->dev,"%s: Failed to get map fd=%d\n" , - __func__, fd); - } - - err =fastrpc_map_attach (fl, fd, len, attr, ppmap); +if (!fastrpc_map_lookup (fl, fd, ppmap, true)) +return 0 ; -return err; +return fastrpc_map_attach (fl, fd, len, attr, ppmap); }/*
@@ -1153,7 +1150,7 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,for (i =0 ; i <FASTRPC_MAX_FDLIST ; i++) {if (!fdlist[i])break ; -if (!fastrpc_map_lookup (fl, (int )fdlist[i], &mmap)) +if (!fastrpc_map_lookup (fl, (int )fdlist[i], &mmap, false))fastrpc_map_put (mmap); }
Signature · 1 line
2.50.1 (Apple Git-155)
Srinivas Kandagatla
maintainerThanks for the patch,
Quoted 6 lines · Zhenghang Xiao
fastrpc_map_lookupreturns a raw pointer after releasingfl->lock. The callerfastrpc_map_createthen callsfastrpc_map_get(kref_get_unless_zero) on this unprotected pointer. A concurrentMEM_UNMAPcan free the map between the lock release and the kref operation, resulting in a use-after-free on the freed slab object.
Quoted 6 lines
Restore the
take_refparameter tofastrpc_map_lookupso the reference is acquired atomically underfl->lockbefore the pointer is exposed to the caller.Fixes 10df039834f8 ("misc: fastrpc: Skip reference for DMA handles")Signed-off-by Zhenghang Xiao <[email protected]>
Patch itself looks sane to me, This needs cc: Stable
Quoted 62 lines
--- drivers/misc/fastrpc.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)drivers/misc/fastrpc.c +11
-14@@ -386,7 +386,7 @@ static int fastrpc_map_get(struct fastrpc_map *map)static int fastrpc_map_lookup (struct fastrpc_user *fl,int fd, -struct fastrpc_map **ppmap) +struct fastrpc_map **ppmap,bool take_ref) {struct fastrpc_map *map =NULL ;struct dma_buf *buf;@@ -401,6 +401,12 @@ static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,if (map->fd != fd || map->buf != buf)continue ; +if (take_ref) { + ret =fastrpc_map_get (map); +if (ret) +break ; + } + *ppmap = map; ret =0 ;break ;@@ -871,19 +877,10 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,static int fastrpc_map_create (struct fastrpc_user *fl,int fd,u64 len,u32 attr,struct fastrpc_map **ppmap) { -struct fastrpc_session_ctx *sess = fl->sctx; -int err =0 ; - -if (!fastrpc_map_lookup (fl, fd, ppmap)) { -if (!fastrpc_map_get (*ppmap)) -return 0 ; -dev_dbg (sess->dev,"%s: Failed to get map fd=%d\n" , - __func__, fd); - } - - err =fastrpc_map_attach (fl, fd, len, attr, ppmap); +if (!fastrpc_map_lookup (fl, fd, ppmap, true)) +return 0 ; -return err; +return fastrpc_map_attach (fl, fd, len, attr, ppmap); }/* @@ -1153,7 +1150,7 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,for (i =0 ; i <FASTRPC_MAX_FDLIST ; i++) {if (!fdlist[i])break ; -if (!fastrpc_map_lookup (fl, (int )fdlist[i], &mmap)) +if (!fastrpc_map_lookup (fl, (int )fdlist[i], &mmap, false))fastrpc_map_put (mmap); }
Quoted 11 lines · Zhenghang Xiao
fastrpc_map_lookupreturns a raw pointer after releasingfl->lock. The callerfastrpc_map_createthen callsfastrpc_map_get(kref_get_unless_zero) on this unprotected pointer. A concurrentMEM_UNMAPcan free the map between the lock release and the kref operation, resulting in a use-after-free on the freed slab object.Restore the
take_refparameter tofastrpc_map_lookupso the reference is acquired atomically underfl->lockbefore the pointer is exposed to the caller.[...]
Applied, thanks!
[1/1] misc: fastrpc: fix use-after-free race in fastrpc_map_create
commit: 052a0d88ef8f02128976db347112307c3bc4d1da
Best regards,
Signature · 1 line
Srinivas Kandagatla <[email protected]>