5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mikulas Patocka commit 406d576cc457203cf86eac7f17839b2efec81db4 upstream. If kobject_init_and_add fails, we must call kobject_put (see the comment at kobject_init_and_add). Before calling kobject_init_and_add, we must zero the kobject (see https://lwn.net/Articles/52085/). If the first call to dm_sysfs_init failed, it may be called again, and that second call would see an uninitialized kobject. Cc: stable@vger.kernel.org Fixes: 784aae735d9b ("dm: add name and uuid to sysfs") Signed-off-by: Mikulas Patocka Reported-by: Wentao Liang Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-sysfs.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) --- a/drivers/md/dm-sysfs.c +++ b/drivers/md/dm-sysfs.c @@ -130,9 +130,20 @@ static struct kobj_type dm_ktype = { */ int dm_sysfs_init(struct mapped_device *md) { - return kobject_init_and_add(dm_kobject(md), &dm_ktype, - &disk_to_dev(dm_disk(md))->kobj, - "%s", "dm"); + struct kobject *kobj = dm_kobject(md); + int r; + + /* The kobject must be zeroed before calling kobject_init */ + memset(kobj, 0, sizeof(struct kobject)); + r = kobject_init_and_add(kobj, &dm_ktype, + &disk_to_dev(dm_disk(md))->kobj, + "%s", "dm"); + if (unlikely(r)) { + /* Weird semantics - we must call kobject_put on failure */ + dm_sysfs_exit(md); + return r; + } + return 0; } /*