Shang XiaoJing 4634c97309 chardev: Fix potential memory leak when cdev_add() failed
Some init function of cdev(like comedi) will call kobject_set_name()
before cdev_add(), but won't free the cdev.kobj.name or put the ref cnt
of cdev.kobj when cdev_add() failed. As the result, cdev.kobj.name will
be leaked.

Free the name of kobject in cdev_add() fail path to prevent memleak. With
this fix, the callers don't need to care about freeing the name of
kobject if cdev_add() fails.

unreferenced object 0xffff8881000fa8c0 (size 8):
  comm "modprobe", pid 239, jiffies 4294905173 (age 51.308s)
  hex dump (first 8 bytes):
    63 6f 6d 65 64 69 00 ff                          comedi..
  backtrace:
    [<000000005f9878f7>] __kmalloc_node_track_caller+0x4c/0x1c0
    [<000000000fd70302>] kstrdup+0x3f/0x70
    [<000000009428bc33>] kstrdup_const+0x46/0x60
    [<00000000ed50d9de>] kvasprintf_const+0xdb/0xf0
    [<00000000b2766964>] kobject_set_name_vargs+0x3c/0xe0
    [<00000000f2424ef7>] kobject_set_name+0x62/0x90
    [<000000005d5a125b>] 0xffffffffa0013098
    [<00000000f331e663>] do_one_initcall+0x7a/0x380
    [<00000000aa7bac96>] do_init_module+0x5c/0x230
    [<000000005fd72335>] load_module+0x227d/0x2420
    [<00000000ad550cf1>] __do_sys_finit_module+0xd5/0x140
    [<00000000069a60c5>] do_syscall_64+0x3f/0x90
    [<00000000c5e0d521>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

Suggested-by: Greg KH <gregkh@linuxfoundation.org>
Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
Link: https://lore.kernel.org/r/20221102072659.23671-1-shangxiaojing@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-11-10 18:38:33 +01:00
..
2022-07-02 18:52:21 +09:00
2022-10-06 17:13:18 -07:00
2022-10-18 11:25:50 -07:00
2022-10-04 13:38:03 -07:00
2022-10-10 19:45:17 -07:00
2022-10-10 20:13:22 -07:00
2022-05-09 16:21:45 -04:00
2022-10-10 19:45:17 -07:00
2022-08-03 10:35:43 -07:00
2022-10-07 08:19:26 -07:00
2022-09-24 07:00:00 +02:00
2022-10-13 09:58:42 -07:00
2022-10-21 15:51:30 -07:00
2022-10-10 19:45:17 -07:00
2022-10-10 19:45:17 -07:00
2022-05-09 16:21:46 -04:00
2022-10-10 19:45:17 -07:00
2022-09-11 20:26:07 -07:00
2022-10-06 17:36:48 -07:00
2022-08-11 13:11:49 -07:00
2022-09-24 07:00:00 +02:00
2022-08-20 11:34:33 -04:00
2022-10-10 19:45:17 -07:00
2022-10-29 17:49:33 -07:00
2022-06-29 13:07:16 +02:00
2022-10-06 16:49:00 -07:00
2022-04-01 19:35:56 -07:00
2022-10-10 14:21:11 -07:00
2022-08-03 10:35:43 -07:00
2022-10-10 19:45:17 -07:00
2022-10-06 17:13:18 -07:00
2022-06-28 13:58:05 -04:00
2022-05-22 21:03:01 +01:00