[PATCH] misc: fastrpc: Fix double free of 'buf' in error path

From: Sukrut Bellary
Date: Thu May 18 2023 - 06:09:16 EST


smatch warning:
drivers/misc/fastrpc.c:1926 fastrpc_req_mmap() error: double free of 'buf'

In fastrpc_req_mmap() error path, the fastrpc buffer is freed in
fastrpc_req_munmap_impl() if unmap is successful.

But in the end, there is an unconditional call to fastrpc_buf_free().
So the above case triggers the double free of fastrpc buf.

Fix this by avoiding the call to the second fastrpc_buf_free() if
fastrpc_req_munmap_impl() is successful.
'err' is not updated as it needs to retain the err returned by
qcom_scm_assign_mem(), which is the starting point of this error path.

This is based on static analysis only. Compilation tested.

Reviewed-by: Shuah Khan <skhan@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Sukrut Bellary <sukrut.bellary@xxxxxxxxx>
---
drivers/misc/fastrpc.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index f48466960f1b..1c3ab78f274f 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -1921,7 +1921,10 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
return 0;

err_assign:
- fastrpc_req_munmap_impl(fl, buf);
+ if (!fastrpc_req_munmap_impl(fl, buf)) {
+ /* buf is freed */
+ return err;
+ }
err_invoke:
fastrpc_buf_free(buf);

--
2.34.1