Skip to contentSkip to conversation

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

PATCH Applied Sukrut Bellary 3 participants 6 messages Jun 2, 2023 – Aug 16, 2024

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

PATCH v2 1/1 misc: fastrpc: Fix double free of 'buf' in error path

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.

Fixes 72fa6f7820c4 ("misc: fastrpc: Rework fastrpc_req_munmap")

---
This is based on static analysis. Compilation tested.
---
Changes in v2:
- Fixed the commit message.
- Addressed the review comment about deleting buf from the list
before freeing.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 drivers/misc/fastrpc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/misc/fastrpc.c +2 -3
index f48466960f1b..b3b520fcfb75 100644
@@ -1882,7 +1882,8 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
 				      &args[0]);
 	if (err) {
 		dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
-		goto err_invoke;
+		fastrpc_buf_free(buf);
+		return err;
 	}
 
 	/* update the buffer to be able to deallocate the memory on the DSP */
@@ -1922,8 +1923,6 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
 
 err_assign:
 	fastrpc_req_munmap_impl(fl, buf);
-err_invoke:
-	fastrpc_buf_free(buf);
 
 	return err;
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Sukrut Bellary

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.

Quoted 7 lines

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

Fixes 72fa6f7820c4 ("misc: fastrpc: Rework fastrpc_req_munmap")

Not sure how was this reviewed-by picked up on this patch.

Suggested-by will be a right choice here.

Will manually edit before applying it.

--srini

Quoted 36 lines

---
This is based on static analysis. Compilation tested.
---
Changes in v2:
- Fixed the commit message.
- Addressed the review comment about deleting buf from the list
before freeing.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
  drivers/misc/fastrpc.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/misc/fastrpc.c +2 -3
index f48466960f1b..b3b520fcfb75 100644
@@ -1882,7 +1882,8 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
  				      &args[0]);
  	if (err) {
  		dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
-		goto err_invoke;
+		fastrpc_buf_free(buf);
+		return err;
  	}
 
  	/* update the buffer to be able to deallocate the memory on the DSP */
@@ -1922,8 +1923,6 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
 
  err_assign:
  	fastrpc_req_munmap_impl(fl, buf);
-err_invoke:
-	fastrpc_buf_free(buf);
 
  	return err;
  }
#2 lore raw
In reply to Sukrut Bellary
Quoted 10 lines · Sukrut Bellary

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.

[...]

Applied, thanks!

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

      commit: 5b3006fffce89706a12741d7f657869ca2be1640

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>
#3 lore raw
Quoted 11 lines · Srinivas Kandagatla
Quoted 9 lines · Sukrut Bellary

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.

Quoted 7 lines
Quoted 1 line

[...]

Applied, thanks!

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

      commit: 5b3006fffce89706a12741d7f657869ca2be1640

This patch wasn't actually applied. I'm not sure what went wrong.

regards,
dan carpenter

#4 lore raw
Quoted 14 lines · Dan Carpenter
Quoted 13 lines · Srinivas Kandagatla
Quoted 10 lines · Sukrut Bellary

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.

[...]

Quoted 7 lines
Quoted 5 lines

Applied, thanks!

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

       commit: 5b3006fffce89706a12741d7f657869ca2be1640

This patch wasn't actually applied. I'm not sure what went wrong.

Am not sure either, its now applied!

thanks for checking this.

--srini

Quoted 4 lines

regards,
dan carpenter

#5 lore raw
In reply to Sukrut Bellary
Quoted 10 lines · Sukrut Bellary

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.

[...]

Applied, thanks!

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

      commit: b056e4e23acbbbf12fa011caa781f7cecbd5b311

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>