Skip to contentSkip to conversation

comedi: Fix potential memory leak in comedi_init()

PATCH In review Shang XiaoJing 3 participants 11 messages Nov 1, 2022 – Nov 2, 2022 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 comedi: Fix potential memory leak in comedi_init()

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")
---
 drivers/comedi/comedi_fops.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +4 -1
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
 
 	retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
 			  COMEDI_NUM_MINORS);
-	if (retval)
+	if (retval) {
+		kfree_const(comedi_cdev.kobj.name);
+		comedi_cdev.kobj.name = NULL;
 		goto out_unregister_chrdev_region;
+	}
 
 	comedi_class = class_create(THIS_MODULE, "comedi");
 	if (IS_ERR(comedi_class)) {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 35 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")
---
 drivers/comedi/comedi_fops.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index e2114bcf815a..2c508c2cf6f6 100644

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 9 lines
drivers/comedi/comedi_fops.c +4 -1
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
 
 	retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
 			  COMEDI_NUM_MINORS);
-	if (retval)
+	if (retval) {
+		kfree_const(comedi_cdev.kobj.name);
+		comedi_cdev.kobj.name = NULL;
 		goto out_unregister_chrdev_region;
+	}

A driver should never have to poke around in the internals of a cdev object like this. Please fix the cdev core to not need this if cdev_add() fails.

thanks,

greg k-h

#2 lore raw
Quoted 37 lines · Greg KH
Quoted 36 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")
---
  drivers/comedi/comedi_fops.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
 
Quoted 13 lines

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 8 lines
drivers/comedi/comedi_fops.c +4 -1
  	retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
  			  COMEDI_NUM_MINORS);
-	if (retval)
+	if (retval) {
+		kfree_const(comedi_cdev.kobj.name);
+		comedi_cdev.kobj.name = NULL;
  		goto out_unregister_chrdev_region;
+	}

A driver should never have to poke around in the internals of a cdev object like this. Please fix the cdev core to not need this if cdev_add() fails.

Hi,

I had checked all 67 places that used cdev_add(), and only the following 5 functions set the name before the cdev_add():

1. comedi_init(), will memleak and will be fixed by this patch.
2. hfi1_cdev_init(), won't memleak.
3. qib_cdev_init(), won't memleak.
4. uio_major_init(), won't memleak.
5. __register_chrdev(), won't memleak.

As the result, I just fix the code in comedi_init(). Should I still fix the cdev core code, which is free the name in cdev_add() fail path?

Thanks,

Signature · 1 line
Shang XiaoJing
maintainer
#3 lore raw
Quoted 43 lines · shangxiaojing
Quoted 40 lines · Greg KH
Quoted 39 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -1
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
Quoted 8 lines
Quoted 8 lines

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 4 lines
drivers/comedi/comedi_fops.c +3 -0
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

Quoted 5 lines
Quoted 1 line

Hi,

I had checked all 67 places that used cdev_add(), and only the following 5 functions set the name before the cdev_add():

Quoted 7 lines

1. comedi_init(), will memleak and will be fixed by this patch.
2. hfi1_cdev_init(), won't memleak.
3. qib_cdev_init(), won't memleak.
4. uio_major_init(), won't memleak.
5. __register_chrdev(), won't memleak.

As the result, I just fix the code in comedi_init(). Should I still fix the cdev core code, which is free the name in cdev_add() fail path?

I'm stuck trying to work out why hfi1_cdev_init() doesn't have the same problem when cdev_add() returns an error.

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#4 lore raw
Quoted 53 lines · Ian Abbott
Quoted 43 lines · shangxiaojing
Quoted 40 lines · Greg KH
Quoted 39 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -1
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
Quoted 8 lines
Quoted 8 lines

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 4 lines
drivers/comedi/comedi_fops.c +3 -0
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 1 line

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

It's ok to add a pair function, but maybe only one place where used cdev_add() need free name. Your mean all "kfree_const(name);" should be replaced to the new function?

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

Hi,

I had checked all 67 places that used cdev_add(), and only the following 5 functions set the name before the cdev_add():

1. comedi_init(), will memleak and will be fixed by this patch.
2. hfi1_cdev_init(), won't memleak.
3. qib_cdev_init(), won't memleak.
4. uio_major_init(), won't memleak.
5. __register_chrdev(), won't memleak.

Quoted 4 lines
Quoted 1 line

As the result, I just fix the code in comedi_init(). Should I still fix the cdev core code, which is free the name in cdev_add() fail path?

I'm stuck trying to work out why hfi1_cdev_init() doesn't have the same problem when cdev_add() returns an error.

Only user_add() calls the hfi1_cdev_init(), and will call user_remove() if hfi1_cdev_init() failed. In user_remove(), hfi1_cdev_cleanup() will call cdev_del().

Thanks,

Signature · 1 line
Shang XiaoJing
maintainer
#5 lore raw
Quoted 72 lines · shangxiaojing
Quoted 53 lines · Ian Abbott
Quoted 43 lines · shangxiaojing
Quoted 40 lines · Greg KH
Quoted 39 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -1
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
Quoted 8 lines
Quoted 8 lines

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 4 lines
drivers/comedi/comedi_fops.c +3 -0
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 1 line

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

It's ok to add a pair function, but maybe only one place where used cdev_add() need free name. Your mean all "kfree_const(name);" should be replaced to the new function?

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

Hi,

I had checked all 67 places that used cdev_add(), and only the following 5 functions set the name before the cdev_add():

1. comedi_init(), will memleak and will be fixed by this patch.
2. hfi1_cdev_init(), won't memleak.
3. qib_cdev_init(), won't memleak.
4. uio_major_init(), won't memleak.
5. __register_chrdev(), won't memleak.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

As the result, I just fix the code in comedi_init(). Should I still fix the cdev core code, which is free the name in cdev_add() fail path?

I'm stuck trying to work out why hfi1_cdev_init() doesn't have the same problem when cdev_add() returns an error.

Only user_add() calls the hfi1_cdev_init(), and will call user_remove() if hfi1_cdev_init() failed. In user_remove(), hfi1_cdev_cleanup() will call cdev_del().

No it won't. hfi1_cdev_cleanup() only calls cdev_del() if device is non-null, but device will be null if the call to cdev_add() failed in hfi1_cdev_init().

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#6 lore raw
Quoted 73 lines · Ian Abbott
Quoted 72 lines · shangxiaojing
Quoted 53 lines · Ian Abbott
Quoted 43 lines · shangxiaojing
Quoted 40 lines · Greg KH
Quoted 39 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -1
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
Quoted 8 lines
Quoted 8 lines

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 4 lines
drivers/comedi/comedi_fops.c +3 -0
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 1 line

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

It's ok to add a pair function, but maybe only one place where used cdev_add() need free name. Your mean all "kfree_const(name);" should be replaced to the new function?

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

Hi,

I had checked all 67 places that used cdev_add(), and only the following 5 functions set the name before the cdev_add():

1. comedi_init(), will memleak and will be fixed by this patch.
2. hfi1_cdev_init(), won't memleak.
3. qib_cdev_init(), won't memleak.
4. uio_major_init(), won't memleak.
5. __register_chrdev(), won't memleak.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

As the result, I just fix the code in comedi_init(). Should I still fix the cdev core code, which is free the name in cdev_add() fail path?

I'm stuck trying to work out why hfi1_cdev_init() doesn't have the same problem when cdev_add() returns an error.

Only user_add() calls the hfi1_cdev_init(), and will call user_remove() if hfi1_cdev_init() failed. In user_remove(), hfi1_cdev_cleanup() will call cdev_del().

No it won't.  hfi1_cdev_cleanup() only calls cdev_del() if device is non-null, but device will be null if the call to cdev_add() failed in hfi1_cdev_init().

Right, device won't be inited, so there has the same potential memleak.

Thanks,

Signature · 1 line
Shang XiaoJing
maintainer
#7 lore raw
In reply to Ian Abbott
Quoted 53 lines · Ian Abbott
Quoted 43 lines · shangxiaojing
Quoted 40 lines · Greg KH
Quoted 39 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -1
index e2114bcf815a..2c508c2cf6f6 100644
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
Quoted 8 lines
Quoted 8 lines

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3386

Quoted 4 lines
drivers/comedi/comedi_fops.c +3 -0
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 1 line

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

Looking at it a bit more, cdev_init() calls kobject_init(), and kobject_init() requires a matching call to kobject_put(). Nothing is calling kobject_put() in this situation. Calling cdev_del() will call kobject_put(), so is that the correct way to clean up after cdev_init() if the call to cdev_add() fails?

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#8 lore raw
Quoted 54 lines · Ian Abbott
Quoted 53 lines · Ian Abbott
Quoted 51 lines · shangxiaojing
Quoted 48 lines · Greg KH
Quoted 35 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index e2114bcf815a..2c508c2cf6f6 100644

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3385

Quoted 8 lines
drivers/comedi/comedi_fops.c +4 -1
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 4 lines
Quoted 1 line

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

Looking at it a bit more, cdev_init() calls kobject_init(), and kobject_init() requires a matching call to kobject_put().  Nothing is calling kobject_put() in this situation.  Calling cdev_del() will call kobject_put(), so is that the correct way to clean up after cdev_init() if the call to cdev_add() fails?

Some cdev call cdev_del() when cdev_add() failed (like init_dvbdev()), and at that time the cdev_unmap() in cdev_del() won't do anything due to the failure of cdev_add(), which is calling the kobject_put() when cdev_add() failed. But I'm not sure which one is better.

Thanks,

Signature · 1 line
Shang XiaoJing
#9 lore raw
Quoted 68 lines · shangxiaojing
Quoted 55 lines · Ian Abbott
Quoted 54 lines · Ian Abbott
Quoted 52 lines · shangxiaojing
Quoted 49 lines · Greg KH
Quoted 36 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")

---
  drivers/comedi/comedi_fops.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index e2114bcf815a..2c508c2cf6f6 100644

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3385

Quoted 8 lines
drivers/comedi/comedi_fops.c +4 -1
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 9 lines
Quoted 2 lines

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

Looking at it a bit more, cdev_init() calls kobject_init(), and kobject_init() requires a matching call to kobject_put().  Nothing is calling kobject_put() in this situation.  Calling cdev_del() will call kobject_put(), so is that the correct way to clean up after cdev_init() if the call to cdev_add() fails?

Quoted 4 lines

Some cdev call cdev_del() when cdev_add() failed (like init_dvbdev()), and at that time the cdev_unmap() in cdev_del() won't do anything due to the failure of cdev_add(), which is calling the kobject_put() when cdev_add() failed. But I'm not sure which one is better.

My point is that no one calling the cdev_add() call should have to do this type of "cleanup" The cdev code should do it automatically, we don't want to have to audit every user of cdev_add() today, and in the future for forever, to ensure they got it right.

Let's fix it up in the cdev code itself please. The kobject in a cdev is a very odd thing, it's not the "normal" type of kobject that you expect, so the cdev code should handle all of that on its own and that should not "leak" into any caller.

thanks,

greg k-h

#10 lore raw
Quoted 79 lines · Greg KH
Quoted 68 lines · shangxiaojing
Quoted 55 lines · Ian Abbott
Quoted 54 lines · Ian Abbott
Quoted 52 lines · shangxiaojing
Quoted 49 lines · Greg KH
Quoted 36 lines · Shang XiaoJing

comedi_init() will goto out_unregister_chrdev_region if cdev_add() failed, which won't free the resource alloced in kobject_set_name(). Call kfree_const() to free the leaked name before goto out_unregister_chrdev_region.

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
Fixes ed9eccbe8970 ("Staging: add comedi core")
---
   drivers/comedi/comedi_fops.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index e2114bcf815a..2c508c2cf6f6 100644

drivers/comedi/comedi_fops.c static int __init comedi_init(void) · line 3385

Quoted 8 lines
drivers/comedi/comedi_fops.c +4 -1
@@ -3379,8 +3379,11 @@ static int __init comedi_init(void)
       retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
                 COMEDI_NUM_MINORS);
-    if (retval)
+    if (retval) {
+        kfree_const(comedi_cdev.kobj.name);
+        comedi_cdev.kobj.name = NULL;
           goto out_unregister_chrdev_region;
+    }

A driver should never have to poke around in the internals of a cdev object like this.  Please fix the cdev core to not need this if cdev_add() fails.

Quoted 9 lines
Quoted 2 lines

Or at least there should be a function that can be called to undo the allocations of kobject_set_name().

Looking at it a bit more, cdev_init() calls kobject_init(), and kobject_init() requires a matching call to kobject_put().  Nothing is calling kobject_put() in this situation.  Calling cdev_del() will call kobject_put(), so is that the correct way to clean up after cdev_init() if the call to cdev_add() fails?

Quoted 4 lines

Some cdev call cdev_del() when cdev_add() failed (like init_dvbdev()), and at that time the cdev_unmap() in cdev_del() won't do anything due to the failure of cdev_add(), which is calling the kobject_put() when cdev_add() failed. But I'm not sure which one is better.

My point is that no one calling the cdev_add() call should have to do this type of "cleanup" The cdev code should do it automatically, we don't want to have to audit every user of cdev_add() today, and in the future for forever, to ensure they got it right.

Quoted 5 lines

Let's fix it up in the cdev code itself please. The kobject in a cdev is a very odd thing, it's not the "normal" type of kobject that you expect, so the cdev code should handle all of that on its own and that should not "leak" into any caller.

ok, I'll fix in cdev code and send v2.

Thanks,

Signature · 1 line
Shang XiaoJing