Yang Yingliang 8c3e8a6bdb class: fix possible memory leak in __class_register()
If class_add_groups() returns error, the 'cp->subsys' need be
unregister, and the 'cp' need be freed.

We can not call kset_unregister() here, because the 'cls' will
be freed in callback function class_release() and it's also
freed in caller's error path, it will cause double free.

So fix this by calling kobject_del() and kfree_const(name) to
cleanup kobject. Besides, call kfree() to free the 'cp'.

Fault injection test can trigger this:

unreferenced object 0xffff888102fa8190 (size 8):
  comm "modprobe", pid 502, jiffies 4294906074 (age 49.296s)
  hex dump (first 8 bytes):
    70 6b 74 63 64 76 64 00                          pktcdvd.
  backtrace:
    [<00000000e7c7703d>] __kmalloc_track_caller+0x1ae/0x320
    [<000000005e4d70bc>] kstrdup+0x3a/0x70
    [<00000000c2e5e85a>] kstrdup_const+0x68/0x80
    [<000000000049a8c7>] kvasprintf_const+0x10b/0x190
    [<0000000029123163>] kobject_set_name_vargs+0x56/0x150
    [<00000000747219c9>] kobject_set_name+0xab/0xe0
    [<0000000005f1ea4e>] __class_register+0x15c/0x49a

unreferenced object 0xffff888037274000 (size 1024):
  comm "modprobe", pid 502, jiffies 4294906074 (age 49.296s)
  hex dump (first 32 bytes):
    00 40 27 37 80 88 ff ff 00 40 27 37 80 88 ff ff  .@'7.....@'7....
    00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00  .....N..........
  backtrace:
    [<00000000151f9600>] kmem_cache_alloc_trace+0x17c/0x2f0
    [<00000000ecf3dd95>] __class_register+0x86/0x49a

Fixes: ced6473e7486 ("driver core: class: add class_groups support")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Link: https://lore.kernel.org/r/20221026082803.3458760-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-11-09 14:58:29 +01:00
..
2022-10-10 13:28:06 -07:00
2022-10-10 09:10:28 -07:00
2022-10-07 10:48:49 -07:00
2022-10-12 10:23:24 -07:00
2022-10-11 17:42:58 -06:00
2022-10-08 09:46:29 -07:00
2022-10-14 18:41:41 -07:00
2022-10-12 10:23:24 -07:00
2022-10-10 13:59:01 -07:00
2022-10-11 10:53:25 -07:00
2022-10-10 13:20:53 -07:00
2022-10-12 10:23:24 -07:00
2022-10-09 14:05:15 -07:00
2022-10-07 11:24:20 -07:00
2022-10-14 18:41:41 -07:00
2022-10-10 13:13:51 -07:00
2022-10-15 16:36:38 -07:00
2022-10-14 12:38:03 -07:00
2022-10-11 11:08:18 -07:00
2022-10-12 10:23:24 -07:00
2022-10-12 10:35:20 -07:00
2022-10-10 09:10:28 -07:00
2022-10-07 11:32:10 -07:00
2022-10-07 11:24:20 -07:00
2022-10-14 18:36:42 -07:00
2022-10-07 16:13:55 -07:00
2022-10-04 19:36:53 -07:00
2022-10-07 12:33:18 -07:00
2022-10-10 14:02:53 -07:00
2022-10-12 14:46:48 -07:00
2022-10-10 14:02:53 -07:00
2022-10-13 10:31:13 -07:00
2022-10-12 14:39:38 -07:00