Skip to contentSkip to conversation

platform/x86: think-lmi: Fix memory leak when showing current settings

PATCH In review Armin Wolf 2 participants 2 messages Mar 31, 2023 – Apr 3, 2023 x86 platform drivers

lore
Share & tools
lore

Armin Wolf

author
#0 lore raw

PATCH v3 1/1 platform/x86: think-lmi: Fix memory leak when showing current settings

When retriving a item string with tlmi_setting(), the result has to be freed using kfree(). In current_value_show() however, malformed item strings are not freed, causing a memory leak.
Fix this by eliminating the early return responsible for this.

Fixes 0fdf10e5fc96 ("platform/x86: think-lmi: Split current_value to reflect only the value")

---
Changes in v3:
- Fix Fixes: tag
- Add Tested-by: tag
Changes in v2:
- Add Reported-by: and Link: tags

---
 drivers/platform/x86/think-lmi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/x86/think-lmi.c +4 -2
index cc66f7cbccf2..8cafb9d4016c 100644
@@ -930,10 +930,12 @@ static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *a
 	/* validate and split from `item,value` -> `value` */
 	value = strpbrk(item, ",");
 	if (!value || value == item || !strlen(value + 1))
-		return -EINVAL;
+		ret = -EINVAL;
+	else
+		ret = sysfs_emit(buf, "%s\n", value + 1);
 
-	ret = sysfs_emit(buf, "%s\n", value + 1);
 	kfree(item);
+
 	return ret;
 }
 

--
2.30.2

#1 lore raw

Hi,

Quoted 10 lines · Armin Wolf

When retriving a item string with tlmi_setting(), the result has to be freed using kfree(). In current_value_show() however, malformed item strings are not freed, causing a memory leak.
Fix this by eliminating the early return responsible for this.

Fixes 0fdf10e5fc96 ("platform/x86: think-lmi: Split current_value to reflect only the value")

Thank you for your patch, I've applied this patch to my fixes branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=fixes

Note it will show up in my fixes branch once I've pushed my local branch there, which might take a while.

I will include this patch in my next fixes pull-req to Linus for the current kernel development cycle.

Regards,

Hans

Quoted 32 lines

---
Changes in v3:
- Fix Fixes: tag
- Add Tested-by: tag
Changes in v2:
- Add Reported-by: and Link: tags

---
 drivers/platform/x86/think-lmi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/x86/think-lmi.c +4 -3
index cc66f7cbccf2..8cafb9d4016c 100644
@@ -930,10 +930,12 @@ static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *a
 	/* validate and split from `item,value` -> `value` */
 	value = strpbrk(item, ",");
 	if (!value || value == item || !strlen(value + 1))
-		return -EINVAL;
+		ret = -EINVAL;
+	else
+		ret = sysfs_emit(buf, "%s\n", value + 1);
 
-	ret = sysfs_emit(buf, "%s\n", value + 1);
 	kfree(item);
+
 	return ret;
 }
 
--

2.30.2