Skip to contentSkip to conversation

comedi: fix device lifecycle handling in comedi_dev_kref_release()

PATCH NAK Salah Triki 2 participants 4 messages Jan 30 – Feb 4 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 comedi: fix device lifecycle handling in comedi_dev_kref_release()

comedi_dev_kref_release() calls put_device() on dev->class_dev and then immediately frees the comedi_device structure with kfree(). If the device reference count reaches zero, the device model release callback may be invoked, potentially accessing or freeing parts of the structure. Calling kfree() directly after put_device() can cause a use-after-free or double-free.

After device_initialize() and once the struct device is in the device model, all cleanup must go through put_device() and the release callback. The kref release should not free the structure directly, ensuring that comedi_device lifetime is properly managed through device reference counting and preventing potential memory corruption.

Fixes 5b13ed94a7d24 ("staging: comedi: add a kref to comedi device")
---
 drivers/comedi/comedi_fops.c | 1 -
 1 file changed, 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -1
index 2c3eb9e89571..24bb06fe6b45 100644
@@ -118,7 +118,6 @@ static void comedi_dev_kref_release(struct kref *kref)
 
 	mutex_destroy(&dev->mutex);
 	put_device(dev->class_dev);
-	kfree(dev);
 }
 
 /**
Signature · 1 line
2.43.0
maintainer NAK
#1 lore raw
Quoted 25 lines · Salah Triki

comedi_dev_kref_release() calls put_device() on dev->class_dev and then immediately frees the comedi_device structure with kfree(). If the device reference count reaches zero, the device model release callback may be invoked, potentially accessing or freeing parts of the structure. Calling kfree() directly after put_device() can cause a use-after-free or double-free.

After device_initialize() and once the struct device is in the device model, all cleanup must go through put_device() and the release callback. The kref release should not free the structure directly, ensuring that comedi_device lifetime is properly managed through device reference counting and preventing potential memory corruption.

Fixes 5b13ed94a7d24 ("staging: comedi: add a kref to comedi device")
---
  drivers/comedi/comedi_fops.c | 1 -
  1 file changed, 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index 2c3eb9e89571..24bb06fe6b45 100644

drivers/comedi/comedi_fops.c static void comedi_dev_kref_release(struct kref *kref) · line 123

Quoted 7 lines
drivers/comedi/comedi_fops.c +0 -1
@@ -118,7 +118,6 @@ static void comedi_dev_kref_release(struct kref *kref)
 
  	mutex_destroy(&dev->mutex);
  	put_device(dev->class_dev);
-	kfree(dev);
  }
 
  /**

NAK

This isn't freeing the struct device, it is freeing the struct comedi_device. The struct device will be freed by device_create_release() in "drivers/base/core.c".

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 )=-
maintainer
#2 lore raw
Quoted 35 lines · Ian Abbott
Quoted 25 lines · Salah Triki

comedi_dev_kref_release() calls put_device() on dev->class_dev and then immediately frees the comedi_device structure with kfree(). If the device reference count reaches zero, the device model release callback may be invoked, potentially accessing or freeing parts of the structure. Calling kfree() directly after put_device() can cause a use-after-free or double-free.

After device_initialize() and once the struct device is in the device model, all cleanup must go through put_device() and the release callback. The kref release should not free the structure directly, ensuring that comedi_device lifetime is properly managed through device reference counting and preventing potential memory corruption.

Fixes 5b13ed94a7d24 ("staging: comedi: add a kref to comedi device")

---
  drivers/comedi/comedi_fops.c | 1 -

  1 file changed, 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index 2c3eb9e89571..24bb06fe6b45 100644

drivers/comedi/comedi_fops.c static void comedi_dev_kref_release(struct kref · line 122

Quoted 6 lines
drivers/comedi/comedi_fops.c +0 -1
@@ -118,7 +118,6 @@ static void comedi_dev_kref_release(struct kref 
 *kref)
       mutex_destroy(&dev->mutex);
       put_device(dev->class_dev);
-    kfree(dev);
   }
   /**

NAK

Quoted 1 line

This isn't freeing the struct device, it is freeing the struct comedi_device.  The struct device will be freed by device_create_release() in "drivers/base/core.c".

To clarify, the call to put_device() from comedi_dev_kref_release() matches the call to get_device() after a successful call to device_create() from comedi_alloc_board_minor().

The counterpart to comedi_alloc_board_minor() is comedi_free_board_dev() which calls device_destroy() (matching the call to device_create()), followed by a call to comedi_dev_put(). comedi_dev_put() will call comedi_dev_kref_release() via kref_put() when its reference count reaches zero, and as mentioned previously, its call to put_device() matches the previous call to get_device() after the device was created. So there are no hooks back to the comedi module code or the struct comedi_device at that point because the struct device has already been removed from the system.

The reason for the extra calls to get_device() and put_device() is explained by commit 8f988d8784e3 ("staging/comedi: keep reference to class device after destroyed").

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 )=-
#3 lore raw
Quoted 21 lines · Ian Abbott
Quoted 5 lines · Ian Abbott

NAK

This isn't freeing the struct device, it is freeing the struct comedi_device.  The struct device will be freed by device_create_release() in "drivers/base/core.c".

To clarify, the call to put_device() from comedi_dev_kref_release() matches the call to get_device() after a successful call to device_create() from comedi_alloc_board_minor().

The counterpart to comedi_alloc_board_minor() is comedi_free_board_dev() which calls device_destroy() (matching the call to device_create()), followed by a call to comedi_dev_put(). comedi_dev_put() will call comedi_dev_kref_release() via kref_put() when its reference count reaches zero, and as mentioned previously, its call to put_device() matches the previous call to get_device() after the device was created. So there are no hooks back to the comedi module code or the struct comedi_device at that point because the struct device has already been removed from the system.

Quoted 9 lines

The reason for the extra calls to get_device() and put_device() is explained by commit 8f988d8784e3 ("staging/comedi: keep reference to class device after destroyed").

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 )=-

Thanks for the detailed explanation, that makes sense. I’ll drop this patch.

Best regards,
Salah