Skip to content

Commit fdc38ab

Browse files
kuu-rtgregkh
authored andcommitted
platform/x86: think-lmi: Fix kobject cleanup
commit 9110056fe10b0519529bdbbac37311a5037ea0c2 upstream. In tlmi_analyze(), allocated structs with an embedded kobject are freed in error paths after the they were already initialized. Fix this by first by avoiding the initialization of kobjects in tlmi_analyze() and then by correctly cleaning them up in tlmi_release_attr() using their kset's kobject list. Fixes: a40cd7e ("platform/x86: think-lmi: Add WMI interface support on Lenovo platforms") Fixes: 30e7843 ("platform/x86: think-lmi: Split kobject_init() and kobject_add() calls") Cc: [email protected] Reviewed-by: Mark Pearson <[email protected]> Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: Kurt Borja <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ilpo Järvinen <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent dcb76b3 commit fdc38ab

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

drivers/platform/x86/think-lmi.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,19 +1208,22 @@ static struct kobj_attribute debug_cmd = __ATTR_WO(debug_cmd);
12081208
/* ---- Initialisation --------------------------------------------------------- */
12091209
static void tlmi_release_attr(void)
12101210
{
1211+
struct kobject *pos, *n;
12111212
int i;
12121213

12131214
/* Attribute structures */
12141215
for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
12151216
if (tlmi_priv.setting[i]) {
12161217
sysfs_remove_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
1217-
kobject_put(&tlmi_priv.setting[i]->kobj);
12181218
}
12191219
}
12201220
sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
12211221
if (tlmi_priv.can_debug_cmd && debug_support)
12221222
sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &debug_cmd.attr);
12231223

1224+
list_for_each_entry_safe(pos, n, &tlmi_priv.attribute_kset->list, entry)
1225+
kobject_put(pos);
1226+
12241227
kset_unregister(tlmi_priv.attribute_kset);
12251228

12261229
/* Free up any saved signatures */
@@ -1229,19 +1232,17 @@ static void tlmi_release_attr(void)
12291232

12301233
/* Authentication structures */
12311234
sysfs_remove_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
1232-
kobject_put(&tlmi_priv.pwd_admin->kobj);
12331235
sysfs_remove_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
1234-
kobject_put(&tlmi_priv.pwd_power->kobj);
12351236

12361237
if (tlmi_priv.opcode_support) {
12371238
sysfs_remove_group(&tlmi_priv.pwd_system->kobj, &auth_attr_group);
1238-
kobject_put(&tlmi_priv.pwd_system->kobj);
12391239
sysfs_remove_group(&tlmi_priv.pwd_hdd->kobj, &auth_attr_group);
1240-
kobject_put(&tlmi_priv.pwd_hdd->kobj);
12411240
sysfs_remove_group(&tlmi_priv.pwd_nvme->kobj, &auth_attr_group);
1242-
kobject_put(&tlmi_priv.pwd_nvme->kobj);
12431241
}
12441242

1243+
list_for_each_entry_safe(pos, n, &tlmi_priv.authentication_kset->list, entry)
1244+
kobject_put(pos);
1245+
12451246
kset_unregister(tlmi_priv.authentication_kset);
12461247
}
12471248

@@ -1309,8 +1310,8 @@ static int tlmi_sysfs_init(void)
13091310

13101311
/* Build attribute */
13111312
tlmi_priv.setting[i]->kobj.kset = tlmi_priv.attribute_kset;
1312-
ret = kobject_add(&tlmi_priv.setting[i]->kobj, NULL,
1313-
"%s", tlmi_priv.setting[i]->display_name);
1313+
ret = kobject_init_and_add(&tlmi_priv.setting[i]->kobj, &tlmi_attr_setting_ktype,
1314+
NULL, "%s", tlmi_priv.setting[i]->display_name);
13141315
if (ret)
13151316
goto fail_create_attr;
13161317

@@ -1331,7 +1332,8 @@ static int tlmi_sysfs_init(void)
13311332

13321333
/* Create authentication entries */
13331334
tlmi_priv.pwd_admin->kobj.kset = tlmi_priv.authentication_kset;
1334-
ret = kobject_add(&tlmi_priv.pwd_admin->kobj, NULL, "%s", "Admin");
1335+
ret = kobject_init_and_add(&tlmi_priv.pwd_admin->kobj, &tlmi_pwd_setting_ktype,
1336+
NULL, "%s", "Admin");
13351337
if (ret)
13361338
goto fail_create_attr;
13371339

@@ -1340,7 +1342,8 @@ static int tlmi_sysfs_init(void)
13401342
goto fail_create_attr;
13411343

13421344
tlmi_priv.pwd_power->kobj.kset = tlmi_priv.authentication_kset;
1343-
ret = kobject_add(&tlmi_priv.pwd_power->kobj, NULL, "%s", "Power-on");
1345+
ret = kobject_init_and_add(&tlmi_priv.pwd_power->kobj, &tlmi_pwd_setting_ktype,
1346+
NULL, "%s", "Power-on");
13441347
if (ret)
13451348
goto fail_create_attr;
13461349

@@ -1350,7 +1353,8 @@ static int tlmi_sysfs_init(void)
13501353

13511354
if (tlmi_priv.opcode_support) {
13521355
tlmi_priv.pwd_system->kobj.kset = tlmi_priv.authentication_kset;
1353-
ret = kobject_add(&tlmi_priv.pwd_system->kobj, NULL, "%s", "System");
1356+
ret = kobject_init_and_add(&tlmi_priv.pwd_system->kobj, &tlmi_pwd_setting_ktype,
1357+
NULL, "%s", "System");
13541358
if (ret)
13551359
goto fail_create_attr;
13561360

@@ -1359,7 +1363,8 @@ static int tlmi_sysfs_init(void)
13591363
goto fail_create_attr;
13601364

13611365
tlmi_priv.pwd_hdd->kobj.kset = tlmi_priv.authentication_kset;
1362-
ret = kobject_add(&tlmi_priv.pwd_hdd->kobj, NULL, "%s", "HDD");
1366+
ret = kobject_init_and_add(&tlmi_priv.pwd_hdd->kobj, &tlmi_pwd_setting_ktype,
1367+
NULL, "%s", "HDD");
13631368
if (ret)
13641369
goto fail_create_attr;
13651370

@@ -1368,7 +1373,8 @@ static int tlmi_sysfs_init(void)
13681373
goto fail_create_attr;
13691374

13701375
tlmi_priv.pwd_nvme->kobj.kset = tlmi_priv.authentication_kset;
1371-
ret = kobject_add(&tlmi_priv.pwd_nvme->kobj, NULL, "%s", "NVMe");
1376+
ret = kobject_init_and_add(&tlmi_priv.pwd_nvme->kobj, &tlmi_pwd_setting_ktype,
1377+
NULL, "%s", "NVMe");
13721378
if (ret)
13731379
goto fail_create_attr;
13741380

@@ -1406,8 +1412,6 @@ static struct tlmi_pwd_setting *tlmi_create_auth(const char *pwd_type,
14061412
new_pwd->maxlen = tlmi_priv.pwdcfg.core.max_length;
14071413
new_pwd->index = 0;
14081414

1409-
kobject_init(&new_pwd->kobj, &tlmi_pwd_setting_ktype);
1410-
14111415
return new_pwd;
14121416
}
14131417

@@ -1512,7 +1516,6 @@ static int tlmi_analyze(void)
15121516
if (setting->possible_values)
15131517
strreplace(setting->possible_values, ',', ';');
15141518

1515-
kobject_init(&setting->kobj, &tlmi_attr_setting_ktype);
15161519
tlmi_priv.setting[i] = setting;
15171520
kfree(item);
15181521
}

0 commit comments

Comments
 (0)