2022-03-08 08:06:37

by Yeqi Fu

[permalink] [raw]
Subject: [PATCH 2/2] usb: typec: fix memory leak

From: Yeqi Fu <[email protected]>

Resource release is needed on the error handling branch
to prevent memory leak. Fix this by adding kfree to the
error handling branch.

Signed-off-by: Yeqi Fu <[email protected]>
---
drivers/usb/typec/class.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index ee0e520707dd..e210109c696d 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -2099,6 +2099,7 @@ struct typec_port *typec_register_port(struct device *parent,
port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
if (!port->cap) {
put_device(&port->dev);
+ kfree(port);
return ERR_PTR(-ENOMEM);
}

@@ -2106,6 +2107,7 @@ struct typec_port *typec_register_port(struct device *parent,
if (IS_ERR(port->sw)) {
ret = PTR_ERR(port->sw);
put_device(&port->dev);
+ kfree(port);
return ERR_PTR(ret);
}

@@ -2113,6 +2115,7 @@ struct typec_port *typec_register_port(struct device *parent,
if (IS_ERR(port->mux)) {
ret = PTR_ERR(port->mux);
put_device(&port->dev);
+ kfree(port);
return ERR_PTR(ret);
}

@@ -2120,6 +2123,7 @@ struct typec_port *typec_register_port(struct device *parent,
if (ret) {
dev_err(parent, "failed to register port (%d)\n", ret);
put_device(&port->dev);
+ kfree(port);
return ERR_PTR(ret);
}

--
2.30.2


2022-03-08 15:25:24

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [PATCH 2/2] usb: typec: fix memory leak

On Mon, Mar 07, 2022 at 10:56:17PM -0800, Yeqi Fu wrote:
> From: Yeqi Fu <[email protected]>
>
> Resource release is needed on the error handling branch
> to prevent memory leak. Fix this by adding kfree to the
> error handling branch.
>
> Signed-off-by: Yeqi Fu <[email protected]>

NAK.

That resources are released in the release callback - typec_release()
in this case.

> ---
> drivers/usb/typec/class.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index ee0e520707dd..e210109c696d 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -2099,6 +2099,7 @@ struct typec_port *typec_register_port(struct device *parent,
> port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
> if (!port->cap) {
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(-ENOMEM);
> }
>
> @@ -2106,6 +2107,7 @@ struct typec_port *typec_register_port(struct device *parent,
> if (IS_ERR(port->sw)) {
> ret = PTR_ERR(port->sw);
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(ret);
> }
>
> @@ -2113,6 +2115,7 @@ struct typec_port *typec_register_port(struct device *parent,
> if (IS_ERR(port->mux)) {
> ret = PTR_ERR(port->mux);
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(ret);
> }
>
> @@ -2120,6 +2123,7 @@ struct typec_port *typec_register_port(struct device *parent,
> if (ret) {
> dev_err(parent, "failed to register port (%d)\n", ret);
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(ret);
> }

thanks,

--
heikki

2022-03-09 00:38:29

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 2/2] usb: typec: fix memory leak

On Mon, Mar 07, 2022 at 10:56:17PM -0800, Yeqi Fu wrote:
> From: Yeqi Fu <[email protected]>
>
> Resource release is needed on the error handling branch
> to prevent memory leak. Fix this by adding kfree to the
> error handling branch.
>
> Signed-off-by: Yeqi Fu <[email protected]>
> ---
> drivers/usb/typec/class.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index ee0e520707dd..e210109c696d 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -2099,6 +2099,7 @@ struct typec_port *typec_register_port(struct device *parent,
> port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
> if (!port->cap) {
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(-ENOMEM);
> }
>
> @@ -2106,6 +2107,7 @@ struct typec_port *typec_register_port(struct device *parent,
> if (IS_ERR(port->sw)) {
> ret = PTR_ERR(port->sw);
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(ret);
> }
>
> @@ -2113,6 +2115,7 @@ struct typec_port *typec_register_port(struct device *parent,
> if (IS_ERR(port->mux)) {
> ret = PTR_ERR(port->mux);
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(ret);
> }
>
> @@ -2120,6 +2123,7 @@ struct typec_port *typec_register_port(struct device *parent,
> if (ret) {
> dev_err(parent, "failed to register port (%d)\n", ret);
> put_device(&port->dev);
> + kfree(port);
> return ERR_PTR(ret);
> }

How was this tested?