Skip to contentSkip to conversation

platform/x86: think-lmi: Fix reference leak

PATCH Superseded Armin Wolf 3 participants 4 messages Sep 23, 2023 – Sep 25, 2023 x86 platform drivers

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Armin Wolf

author
#0 lore raw

PATCH 1/1 platform/x86: think-lmi: Fix reference leak

If a duplicate attribute is found using kset_find_obj(), a reference to that attribute is returned which needs to be disposed accordingly using kobject_put(). Move the setting name validation into a separate function to allow for this change without having to duplicate the cleanup code for this setting.
As a side note, a very similar bug was fixed in
commit 7295a996fdab ("platform/x86: dell-sysman: Fix reference leak"), so it seems that the bug was copied from that driver.

Compile-tested only.

Fixes 1bcad8e510b2 ("platform/x86: think-lmi: Fix issues with duplicate attributes")
---
 drivers/platform/x86/think-lmi.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/platform/x86/think-lmi.c +20 -4
index 4be6f28d4600..3a396b763c49 100644
@@ -1344,6 +1344,24 @@ static void tlmi_release_attr(void)
 	kset_unregister(tlmi_priv.authentication_kset);
 }
 
+static int tlmi_validate_setting_name(struct kset *attribute_kset, char *name)
+{
+	struct kobject *duplicate;
+
+	if (!strcmp(name, "Reserved"))
+		return -EINVAL;
+
+	duplicate = kset_find_obj(attribute_kset, name);
+	if (duplicate) {
+		pr_debug("Duplicate attribute name found - %s\n", name);
+		/* kset_find_obj() returns a reference */
+		kobject_put(duplicate);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
 static int tlmi_sysfs_init(void)
 {
 	int i, ret;
@@ -1372,10 +1390,8 @@ static int tlmi_sysfs_init(void)
 			continue;
 
 		/* check for duplicate or reserved values */
-		if (kset_find_obj(tlmi_priv.attribute_kset, tlmi_priv.setting[i]->display_name) ||
-		    !strcmp(tlmi_priv.setting[i]->display_name, "Reserved")) {
-			pr_debug("duplicate or reserved attribute name found - %s\n",
-				tlmi_priv.setting[i]->display_name);
+		if (tlmi_validate_setting_name(tlmi_priv.attribute_kset,
+					       tlmi_priv.setting[i]->display_name) < 0) {
 			kfree(tlmi_priv.setting[i]->possible_values);
 			kfree(tlmi_priv.setting[i]);
 			tlmi_priv.setting[i] = NULL;

--
2.39.2

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 49 lines · Armin Wolf

If a duplicate attribute is found using kset_find_obj(), a reference to that attribute is returned which needs to be disposed accordingly using kobject_put(). Move the setting name validation into a separate function to allow for this change without having to duplicate the cleanup code for this setting.
As a side note, a very similar bug was fixed in
commit 7295a996fdab ("platform/x86: dell-sysman: Fix reference leak"), so it seems that the bug was copied from that driver.

Compile-tested only.

Fixes 1bcad8e510b2 ("platform/x86: think-lmi: Fix issues with duplicate attributes")
---
 drivers/platform/x86/think-lmi.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/platform/x86/think-lmi.c +18 -0
index 4be6f28d4600..3a396b763c49 100644
@@ -1344,6 +1344,24 @@ static void tlmi_release_attr(void)
 	kset_unregister(tlmi_priv.authentication_kset);
 }
 
+static int tlmi_validate_setting_name(struct kset *attribute_kset, char *name)
+{
+	struct kobject *duplicate;
+
+	if (!strcmp(name, "Reserved"))
+		return -EINVAL;
+
+	duplicate = kset_find_obj(attribute_kset, name);
+	if (duplicate) {
+		pr_debug("Duplicate attribute name found - %s\n", name);
+		/* kset_find_obj() returns a reference */
+		kobject_put(duplicate);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
 static int tlmi_sysfs_init(void)
 {
 	int i, ret;
@@ -1372,10 +1390,8 @@ static int tlmi_sysfs_init(void)
 			continue;
 

drivers/platform/x86/think-lmi.c static int tlmi_sysfs_init(void) · line 1397

Quoted 10 lines
drivers/platform/x86/think-lmi.c +2 -4
 		/* check for duplicate or reserved values */
-		if (kset_find_obj(tlmi_priv.attribute_kset, tlmi_priv.setting[i]->display_name) ||
-		    !strcmp(tlmi_priv.setting[i]->display_name, "Reserved")) {
-			pr_debug("duplicate or reserved attribute name found - %s\n",
-				tlmi_priv.setting[i]->display_name);
+		if (tlmi_validate_setting_name(tlmi_priv.attribute_kset,
+					       tlmi_priv.setting[i]->display_name) < 0) {
 			kfree(tlmi_priv.setting[i]->possible_values);
 			kfree(tlmi_priv.setting[i]);
 			tlmi_priv.setting[i] = NULL;

There seem to be two more of these in hp-bioscfg.

Signature · 1 line
 i.
Reviewed-by on v1 1/1
#2 lore raw
In reply to Armin Wolf

Thanks Armin,

Quoted 60 lines · Armin Wolf

If a duplicate attribute is found using kset_find_obj(), a reference to that attribute is returned which needs to be disposed accordingly using kobject_put(). Move the setting name validation into a separate function to allow for this change without having to duplicate the cleanup code for this setting.
As a side note, a very similar bug was fixed in
commit 7295a996fdab ("platform/x86: dell-sysman: Fix reference leak"), so it seems that the bug was copied from that driver.

Compile-tested only.

Fixes 1bcad8e510b2 ("platform/x86: think-lmi: Fix issues with duplicate attributes")
---
  drivers/platform/x86/think-lmi.c | 24 ++++++++++++++++++++----
  1 file changed, 20 insertions(+), 4 deletions(-)
drivers/platform/x86/think-lmi.c +20 -5
index 4be6f28d4600..3a396b763c49 100644
@@ -1344,6 +1344,24 @@ static void tlmi_release_attr(void)
  	kset_unregister(tlmi_priv.authentication_kset);
  }
 
+static int tlmi_validate_setting_name(struct kset *attribute_kset, char *name)
+{
+	struct kobject *duplicate;
+
+	if (!strcmp(name, "Reserved"))
+		return -EINVAL;
+
+	duplicate = kset_find_obj(attribute_kset, name);
+	if (duplicate) {
+		pr_debug("Duplicate attribute name found - %s\n", name);
+		/* kset_find_obj() returns a reference */
+		kobject_put(duplicate);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
  static int tlmi_sysfs_init(void)
  {
  	int i, ret;
@@ -1372,10 +1390,8 @@ static int tlmi_sysfs_init(void)
  			continue;
 
  		/* check for duplicate or reserved values */
-		if (kset_find_obj(tlmi_priv.attribute_kset, tlmi_priv.setting[i]->display_name) ||
-		    !strcmp(tlmi_priv.setting[i]->display_name, "Reserved")) {
-			pr_debug("duplicate or reserved attribute name found - %s\n",
-				tlmi_priv.setting[i]->display_name);
+		if (tlmi_validate_setting_name(tlmi_priv.attribute_kset,
+					       tlmi_priv.setting[i]->display_name) < 0) {
  			kfree(tlmi_priv.setting[i]->possible_values);
  			kfree(tlmi_priv.setting[i]);
  			tlmi_priv.setting[i] = NULL;
--
Quoted 2 lines

2.39.2

Do you have any reports of our platforms where these are seen? If so I'd like to get it fixed in FW too (and I can get it tested on HW if that helps)

Mark

author
#3 lore raw

Am 25.09.23 um 16:00 schrieb Mark Pearson:

Quoted 73 lines

Thanks Armin,

Quoted 58 lines · Armin Wolf

If a duplicate attribute is found using kset_find_obj(), a reference to that attribute is returned which needs to be disposed accordingly using kobject_put(). Move the setting name validation into a separate function to allow for this change without having to duplicate the cleanup code for this setting.
As a side note, a very similar bug was fixed in
commit 7295a996fdab ("platform/x86: dell-sysman: Fix reference leak"), so it seems that the bug was copied from that driver.

Compile-tested only.

Fixes 1bcad8e510b2 ("platform/x86: think-lmi: Fix issues with

duplicate attributes")

---
  drivers/platform/x86/think-lmi.c | 24 ++++++++++++++++++++----

  1 file changed, 20 insertions(+), 4 deletions(-)
drivers/platform/x86/think-lmi.c +18 -0
index 4be6f28d4600..3a396b763c49 100644
@@ -1344,6 +1344,24 @@ static void tlmi_release_attr(void)
       kset_unregister(tlmi_priv.authentication_kset);
   }
 
+static int tlmi_validate_setting_name(struct kset *attribute_kset,
 char *name)
+{
+    struct kobject *duplicate;
+
+    if (!strcmp(name, "Reserved"))
+        return -EINVAL;
+
+    duplicate = kset_find_obj(attribute_kset, name);
+    if (duplicate) {
+        pr_debug("Duplicate attribute name found - %s\n", name);
+        /* kset_find_obj() returns a reference */
+        kobject_put(duplicate);
+        return -EBUSY;
+    }
+
+    return 0;
+}
+
   static int tlmi_sysfs_init(void)
   {

      int i, ret;

drivers/platform/x86/think-lmi.c +0 -1
@@ -1372,10 +1390,8 @@ static int tlmi_sysfs_init(void)
               continue;
 
           /* check for duplicate or reserved values */
-        if (kset_find_obj(tlmi_priv.attribute_kset,

tlmi_priv.setting[i]->display_name) ||
-            !strcmp(tlmi_priv.setting[i]->display_name, "Reserved")) { -            pr_debug("duplicate or reserved attribute name found - %s\n",

Quoted 9 lines

-                tlmi_priv.setting[i]->display_name);
+        if (tlmi_validate_setting_name(tlmi_priv.attribute_kset,
+ tlmi_priv.setting[i]->display_name) < 0) { kfree(tlmi_priv.setting[i]->possible_values); kfree(tlmi_priv.setting[i]); tlmi_priv.setting[i] = NULL;
--
2.39.2

Quoted 6 lines

Do you have any reports of our platforms where these are seen? If so I'd like to get it fixed in FW too (and I can get it tested on HW if that helps)

Mark

No, i do not have any reports regarding this issue. I stumbled upon this bug by chance, after fixing a similar bug in dell-wmi-sysman. I suspect that the bug was copied from there to the other drivers, like think-lmi and hp-bioscfg.

Armin Wolf