linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] misc: fastrpc: Fix double free of 'buf' in error path
@ 2023-05-18 10:08 Sukrut Bellary
  2023-05-18 10:55 ` Dan Carpenter
  0 siblings, 1 reply; 14+ messages in thread
From: Sukrut Bellary @ 2023-05-18 10:08 UTC (permalink / raw)
  To: Srinivas Kandagatla, Amol Maheshwari, Arnd Bergmann,
	Greg Kroah-Hartman
  Cc: Sukrut Bellary, linux-arm-msm, linux-kernel, kernel-janitors,
	Shuah Khan

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@linuxfoundation.org>
Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
---
 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


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-06-01 19:10 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-18 10:08 [PATCH] misc: fastrpc: Fix double free of 'buf' in error path Sukrut Bellary
2023-05-18 10:55 ` Dan Carpenter
2023-05-19  2:45   ` Sukrut Bellary
2023-05-19  4:16     ` Dan Carpenter
2023-05-19  6:12       ` Sukrut Bellary
2023-05-19  9:52   ` Srinivas Kandagatla
2023-05-19 10:22     ` Dan Carpenter
2023-05-19 10:39       ` Srinivas Kandagatla
2023-05-19 22:57         ` Sukrut Bellary
2023-06-01  4:45           ` Sukrut Bellary
2023-06-01  7:00             ` Dan Carpenter
2023-06-01 19:09               ` Sukrut Bellary
2023-05-19 10:58       ` Dan Carpenter
2023-05-19 23:39         ` Sukrut Bellary

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).