2020-04-29 15:32:38

by Srinivas Kandagatla

[permalink] [raw]
Subject: [PATCH] misc: fastrpc: fix memory leak

if misc_register() fails, previously allocated data is left without freeing,
this could result in memory leak.

So fix it!

Signed-off-by: Srinivas Kandagatla <[email protected]>
---
drivers/misc/fastrpc.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index e3e085e33d46..9065d3e71ff7 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -1613,8 +1613,10 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
domains[domain_id]);
data->miscdev.fops = &fastrpc_fops;
err = misc_register(&data->miscdev);
- if (err)
+ if (err) {
+ kfree(data);
return err;
+ }

kref_init(&data->refcount);

--
2.21.0


2020-04-29 20:44:05

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] misc: fastrpc: fix memory leak

> if misc_register() fails, previously allocated data is left without freeing,
> this could result in memory leak.

How do you think about a wording variant like the following?

Subject:
[PATCH v2] misc: fastrpc: Fix an incomplete memory release in fastrpc_rpmsg_probe()

Change description:
A few fastrpc data were not released after a call of the
function “misc_register” failed.
Thus add a call of the function “kfree”.


> So fix it!

Would you like to replace such an information by the tag “Fixes”?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=96c9a7802af7d500a582d89a8b864584fe878c1b#n183

Regards,
Markus

2020-04-30 05:04:18

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH] misc: fastrpc: fix memory leak

On Wed 29 Apr 08:29 PDT 2020, Srinivas Kandagatla wrote:

> if misc_register() fails, previously allocated data is left without freeing,
> this could result in memory leak.

s/could/will/

>
> So fix it!
>

As Markus pointed out, a Fixes: tag would be in order to make sure this
is backported properly.


PS: although unlikely, if of_platform_populate() where to fail we're
leaking both the contet and the misc device.

Regards,
Bjorn

> Signed-off-by: Srinivas Kandagatla <[email protected]>
> ---
> drivers/misc/fastrpc.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index e3e085e33d46..9065d3e71ff7 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -1613,8 +1613,10 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
> domains[domain_id]);
> data->miscdev.fops = &fastrpc_fops;
> err = misc_register(&data->miscdev);
> - if (err)
> + if (err) {
> + kfree(data);
> return err;
> + }
>
> kref_init(&data->refcount);
>
> --
> 2.21.0
>