Skip to contentSkip to conversation

platform/x86: think-lmi: Move kfree(setting->possible_values) to tlmi_attr_setting_release()

PATCH Applied Hans de Goede 1 participant 2 messages Jun 28, 2021 x86 platform drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: think-lmi: Move kfree(setting->possible_values) to tlmi_attr_setting_release()

We must not free the possible_values string before we have called sysfs_remove_group(kobj, &tlmi_attr_group) otherwise there is a race where a sysfs read of possible_values could reference the free-ed memory.

Move the kfree(setting->possible_values) together with the free of the actual tlmi_attr_setting struct to avoid this race.

---
 drivers/platform/x86/think-lmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/think-lmi.c +1 -1
index 4cab341a3538..3671b5d20613 100644
@@ -626,6 +626,7 @@ static void tlmi_attr_setting_release(struct kobject *kobj)
 {
 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
 
+	kfree(setting->possible_values);
 	kfree(setting);
 }
 
@@ -654,7 +655,6 @@ static void tlmi_release_attr(void)
 	/* Attribute structures */
 	for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
 		if (tlmi_priv.setting[i]) {
-			kfree(tlmi_priv.setting[i]->possible_values);
 			sysfs_remove_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
 			kobject_put(&tlmi_priv.setting[i]->kobj);
 		}
Signature · 1 line
2.31.1
author Applied
#1 lore raw

Hi,

Quoted 9 lines · Hans de Goede

We must not free the possible_values string before we have called sysfs_remove_group(kobj, &tlmi_attr_group) otherwise there is a race where a sysfs read of possible_values could reference the free-ed memory.

Move the kfree(setting->possible_values) together with the free of the actual tlmi_attr_setting struct to avoid this race.

I've merged this into my review-hans branch.

Regards,

Hans

Quoted 25 lines
---
 drivers/platform/x86/think-lmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/think-lmi.c +1 -1
index 4cab341a3538..3671b5d20613 100644
@@ -626,6 +626,7 @@ static void tlmi_attr_setting_release(struct kobject *kobj)
 {
 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
 
+	kfree(setting->possible_values);
 	kfree(setting);
 }
 
@@ -654,7 +655,6 @@ static void tlmi_release_attr(void)
 	/* Attribute structures */
 	for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
 		if (tlmi_priv.setting[i]) {
-			kfree(tlmi_priv.setting[i]->possible_values);
 			sysfs_remove_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
 			kobject_put(&tlmi_priv.setting[i]->kobj);
 		}