Skip to contentSkip to conversation

thermal/lib: Fix memory leak on error in thermal_genl_auto()

PATCH Superseded Daniel Lezcano 4 participants 10 messages Oct 24, 2024 Thermal

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal/lib: Fix memory leak on error in thermal_genl_auto()

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

---
 tools/lib/thermal/commands.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
tools/lib/thermal/commands.c +8 -5
index bcf0f14d035a..b0d4c8aca21c 100644
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
 					 struct cmd_param *param,
 					 int cmd, int flags, void *arg)
 {
+	thermal_error_t ret = THERMAL_ERROR;
 	struct nl_msg *msg;
 	void *hdr;
 
 	msg = nlmsg_alloc();
 	if (!msg)
-		return THERMAL_ERROR;
+		goto out;
 
 	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
 			  0, flags, cmd, THERMAL_GENL_VERSION);
 	if (!hdr)
-		return THERMAL_ERROR;
+		goto out;
 
 	if (cmd_cb && cmd_cb(msg, param))
-		return THERMAL_ERROR;
+		goto out;
 
 	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
-		return THERMAL_ERROR;
+		goto out;
 
+	ret = THERMAL_SUCCESS;
+out:
 	nlmsg_free(msg);
 
-	return THERMAL_SUCCESS;
+	return ret;
 }
 
 thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 3 lines · Daniel Lezcano

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

Please note this patch applies on top of the thresholds series.

If nobody complains about the change, I'll take care of applying this patch after the thermal thresholds series appears in the linux-pm branch

Thanks

-- D.

Quoted 46 lines
---
  tools/lib/thermal/commands.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
tools/lib/thermal/commands.c +8 -5
index bcf0f14d035a..b0d4c8aca21c 100644
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
  					 struct cmd_param *param,
  					 int cmd, int flags, void *arg)
  {
+	thermal_error_t ret = THERMAL_ERROR;
  	struct nl_msg *msg;
  	void *hdr;
 
  	msg = nlmsg_alloc();
  	if (!msg)
-		return THERMAL_ERROR;
+		goto out;
 
  	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
  			  0, flags, cmd, THERMAL_GENL_VERSION);
  	if (!hdr)
-		return THERMAL_ERROR;
+		goto out;
 
  	if (cmd_cb && cmd_cb(msg, param))
-		return THERMAL_ERROR;
+		goto out;
 
  	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
-		return THERMAL_ERROR;
+		goto out;
 
+	ret = THERMAL_SUCCESS;
+out:
  	nlmsg_free(msg);
 
-	return THERMAL_SUCCESS;
+	return ret;
  }
 
  thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Daniel Lezcano

Hi Daniel,

Quoted 40 lines · Daniel Lezcano

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

---
  tools/lib/thermal/commands.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
tools/lib/thermal/commands.c +5 -4
index bcf0f14d035a..b0d4c8aca21c 100644
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
  					 struct cmd_param *param,
  					 int cmd, int flags, void *arg)
  {
+	thermal_error_t ret = THERMAL_ERROR;
  	struct nl_msg *msg;
  	void *hdr;
 
  	msg = nlmsg_alloc();
  	if (!msg)
-		return THERMAL_ERROR;
+		goto out;
 
  	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
  			  0, flags, cmd, THERMAL_GENL_VERSION);
  	if (!hdr)
-		return THERMAL_ERROR;
+		goto out;
 
  	if (cmd_cb && cmd_cb(msg, param))
-		return THERMAL_ERROR;
+		goto out;
 
  	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
-		return THERMAL_ERROR;
+		goto out;

tools/lib/thermal/commands.c static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm · line 404

Quoted 10 lines
tools/lib/thermal/commands.c +3 -1
 
+	ret = THERMAL_SUCCESS;
+out:
  	nlmsg_free(msg);
 
-	return THERMAL_SUCCESS;
+	return ret;
  }
 
  thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)

LGTM,

Regards,
Lukasz

#3 lore raw
In reply to Daniel Lezcano
Quoted 3 lines

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145

…

tools/lib/thermal/commands.c static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm · line 384

Quoted 13 lines

+++ b/tools/lib/thermal/commands.c

tools/lib/thermal/commands.c +2 -1
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
 					 struct cmd_param *param,
 					 int cmd, int flags, void *arg)
 {
+	thermal_error_t ret = THERMAL_ERROR;
 	struct nl_msg *msg;
 	void *hdr;
 
 	msg = nlmsg_alloc();
 	if (!msg)
-		return THERMAL_ERROR;
+		goto out;

…

Is it really reasonable to pass a null pointer (from a failed function call) to a subsequent nlmsg_free() call?

Can it be more appropriate to return directly in such an error case?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.12-rc4#n532

Regards,
Markus

#4 lore raw
Quoted 18 lines · Markus Elfring
Quoted 3 lines

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145

…

Quoted 9 lines

+++ b/tools/lib/thermal/commands.c

tools/lib/thermal/commands.c +1 -0
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
  					 struct cmd_param *param,
  					 int cmd, int flags, void *arg)
  {
+	thermal_error_t ret = THERMAL_ERROR;
  	struct nl_msg *msg;
  	void *hdr;
 
Quoted 8 lines

tools/lib/thermal/commands.c static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm · line 384

Quoted 4 lines
tools/lib/thermal/commands.c +1 -1
  	msg = nlmsg_alloc();
  	if (!msg)
-		return THERMAL_ERROR;
+		goto out;

…

Is it really reasonable to pass a null pointer (from a failed function call) to a subsequent nlmsg_free() call?

You are right, I should return here :S

Quoted 5 lines

Can it be more appropriate to return directly in such an error case?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.12-rc4#n532

Regards,
Markus

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#5 lore raw
Quoted 26 lines · Daniel Lezcano
Quoted 24 lines · Markus Elfring
Quoted 3 lines

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145

…

Quoted 13 lines

+++ b/tools/lib/thermal/commands.c

tools/lib/thermal/commands.c +2 -1
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
                                      struct cmd_param *param,
                                      int cmd, int flags, void *arg)
  {
+    thermal_error_t ret = THERMAL_ERROR;
     struct nl_msg *msg;
     void *hdr;
 
     msg = nlmsg_alloc();
     if (!msg)
-            return THERMAL_ERROR;
+            goto out;

…

Quoted 4 lines
Quoted 2 lines

Is it really reasonable to pass a null pointer (from a failed function call) to a subsequent nlmsg_free() call?

You are right, I should return here :S

Do you want to respin it?

Alternatively, I can fix it up when applying the patch.

reviewer
#6 lore raw
Quoted 28 lines · Rafael J. Wysocki
Quoted 26 lines · Daniel Lezcano
Quoted 24 lines · Markus Elfring
Quoted 3 lines

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145

…

Quoted 13 lines

+++ b/tools/lib/thermal/commands.c

tools/lib/thermal/commands.c +2 -1
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
                                       struct cmd_param *param,
                                       int cmd, int flags, void *arg)
   {
+    thermal_error_t ret = THERMAL_ERROR;
      struct nl_msg *msg;
      void *hdr;
 
      msg = nlmsg_alloc();
      if (!msg)
-            return THERMAL_ERROR;
+            goto out;

…

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Is it really reasonable to pass a null pointer (from a failed function call) to a subsequent nlmsg_free() call?

You are right, I should return here :S

Do you want to respin it?

Alternatively, I can fix it up when applying the patch.

The nlmskg_free() function should handle that similar to kfree(). Under the hood there is a check in skb_unref():
if (unlikely(!skb))

AFAIK the kfree() is used similar way and NULL is part of generic case sometimes, not handles with extra code.

Up to you. You can keep my review tag in both cases.

#7 lore raw
In reply to Rafael J. Wysocki
Quoted 28 lines · Rafael J. Wysocki
Quoted 26 lines · Daniel Lezcano
Quoted 24 lines · Markus Elfring
Quoted 3 lines

The function thermal_genl_auto() does not free the allocated message in the error path. Fix that by putting a out label and jump to it which will free the message instead of directly returning an error.

Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145

…

Quoted 13 lines

+++ b/tools/lib/thermal/commands.c

tools/lib/thermal/commands.c +2 -1
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
                                       struct cmd_param *param,
                                       int cmd, int flags, void *arg)
   {
+    thermal_error_t ret = THERMAL_ERROR;
      struct nl_msg *msg;
      void *hdr;
 
      msg = nlmsg_alloc();
      if (!msg)
-            return THERMAL_ERROR;
+            goto out;

…

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Is it really reasonable to pass a null pointer (from a failed function call) to a subsequent nlmsg_free() call?

You are right, I should return here :S

Do you want to respin it?

Alternatively, I can fix it up when applying the patch.

If you don't mind I would prefer to apply the lib patches

For correctness, I'll send a V2 with the return fixed

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#8 lore raw
Quoted 1 line

For correctness, I'll send a V2 with the return fixed

Would you become similarly interested to increase the application of scope-based resource management also for the affected software component?

Regards,
Markus

#9 lore raw
Quoted 4 lines · Markus Elfring
Quoted 1 line

For correctness, I'll send a V2 with the return fixed

Would you become similarly interested to increase the application of scope-based resource management also for the affected software component?

Yes, I know it exist but I did not take the time to read the documentation, neither had the reflex to take care of that. Something I have to do :)

It seems like this function is a good candidate :)

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog