Skip to contentSkip to conversation

staging: comedi: drivers: prevent memory leak

PATCH In review Navid Emamdoost 3 participants 3 messages Sep 17, 2019 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: comedi: drivers: prevent memory leak

In das1800_attach, the buffer allocated via kmalloc_array needs to be released if an error happens.

---
 drivers/staging/comedi/drivers/das1800.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/staging/comedi/drivers/das1800.c +9 -3
index f16aa7e9f4f3..5f2d5f7a6229 100644
@@ -1237,12 +1237,16 @@ static int das1800_attach(struct comedi_device *dev,
 
 	dev->pacer = comedi_8254_init(dev->iobase + DAS1800_COUNTER,
 				      I8254_OSC_BASE_5MHZ, I8254_IO8, 0);
-	if (!dev->pacer)
+	if (!dev->pacer) {
+		kfree(devpriv->fifo_buf);
 		return -ENOMEM;
+	}
 
 	ret = comedi_alloc_subdevices(dev, 4);
-	if (ret)
+	if (ret) {
+		kfree(devpriv->fifo_buf);
 		return ret;
+	}
 
 	/*
 	 * Analog Input subdevice
@@ -1290,8 +1294,10 @@ static int das1800_attach(struct comedi_device *dev,
 		s->insn_write	= das1800_ao_insn_write;
 
 		ret = comedi_alloc_subdev_readback(s);
-		if (ret)
+		if (ret) {
+			kfree(devpriv->fifo_buf);
 			return ret;
+		}
 
 		/* initialize all channels to 0V */
 		for (i = 0; i < s->n_chan; i++) {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 4 lines · Navid Emamdoost

In das1800_attach, the buffer allocated via kmalloc_array needs to be released if an error happens.

Commedit calls ->detach() if the ->attach() fails so this patch would lead to a double free. See comedi_device_attach():

drivers/staging/comedi/drivers.c
983 }
984 if (!driv->attach) {
985 /* driver does not support manual configuration */
986 dev_warn(dev->class_dev,
987 "driver '%s' does not support attach using comedi_config\n",
988 driv->driver_name);

   989                  module_put(driv->module);
   990                  ret = -EIO;
   991                  goto out;
   992          }
   993          dev->driver = driv;
   994          dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
   995                                           : dev->driver->driver_name;
   996          ret = driv->attach(dev, it);
                      ^^^^^^^^^^^^^^^^^^^^^
   997          if (ret >= 0)
   998                  ret = comedi_device_postconfig(dev);
   999          if (ret < 0) {
  1000                  comedi_device_detach(dev);
                        ^^^^^^^^^^^^^^^^^^^^^^^^^

1001 module_put(driv->module);
1002 }
1003 /* On success, the driver module count has been incremented. */

regards,
dan carpenter

#2 lore raw
Quoted 31 lines · Dan Carpenter
Quoted 4 lines · Navid Emamdoost

In das1800_attach, the buffer allocated via kmalloc_array needs to be released if an error happens.

Commedit calls ->detach() if the ->attach() fails so this patch would lead to a double free. See comedi_device_attach():

drivers/staging/comedi/drivers.c

    983          }
    984          if (!driv->attach) {
    985                  /* driver does not support manual configuration */
    986                  dev_warn(dev->class_dev,
    987                           "driver '%s' does not support attach using comedi_config\n",
    988                           driv->driver_name);
    989                  module_put(driv->module);
    990                  ret = -EIO;
    991                  goto out;
    992          }
    993          dev->driver = driv;
    994          dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
    995                                           : dev->driver->driver_name;
    996          ret = driv->attach(dev, it);
                       ^^^^^^^^^^^^^^^^^^^^^
    997          if (ret >= 0)
    998                  ret = comedi_device_postconfig(dev);
    999          if (ret < 0) {
   1000                  comedi_device_detach(dev);
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 3 lines
   1001                  module_put(driv->module);
   1002          }
   1003          /* On success, the driver module count has been incremented. */

Yes, everything should be freed properly by comedi_device_detach(). From comedi_device_detach(), some of the stuff is freed by dev->driver->detach(), and the remainder is freed by comedi_device_detach_cleanup().

Signature · 4 lines
-=( Ian Abbott <[email protected]> || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268.  Registered address:    )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-