Skip to contentSkip to conversation

comedi: comedi_bond: Fix leak of opened device references

PATCH In review Ian Abbott 2 participants 4 messages Nov 3 – Nov 4 Comedi drivers

lore
Share & tools
lore

Ian Abbott

author maintainer
#0 lore raw

PATCH 1/1 comedi: comedi_bond: Fix leak of opened device references

bonding_attach() calls do_dev_config() to open other comedi devices specified by the user and tries to make use of them as part of the 'bonding' device. If it successfully opens the other device but cannot use it (either because it does not any subdevices of a type the 'bonding' device can use, or because of some error that occurs while dealing with its subdevices) then it is left in the open state and the fact that it is open does not get recorded by the 'bonding' device, so the reference to the opened device gets lost. Fix it by closing the opened device if it could not be bound to the 'bonding' device.

To match previous behavior, if the opened device is unsuitable then ignore it (after closing it). The opened devices recorded by the 'bonding' device will eventually get closed by bonding_detach() even if bonding_attach() returns an error.

Fixes 3cf840f65ad5 ("Staging: comedi: add comedi_bond driver")
---
 drivers/comedi/drivers/comedi_bond.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/comedi/drivers/comedi_bond.c +19 -5
index a10cfbd04c728..3764ece90f826 100644
@@ -184,7 +184,9 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 		char file[sizeof("/dev/comediXXXXXX")];
 		int minor = it->options[i];
 		struct comedi_device *d;
-		int sdev = -1, nchans;
+		int sdev = -1;
+		int nchans;
+		int nope = 1;
 		struct bonded_device *bdev;
 		struct bonded_device **devs;
 
@@ -223,11 +225,14 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 				dev_err(dev->class_dev,
 					"comedi_get_n_channels() returned %d on minor %u subdev %d!\n",
 					nchans, minor, sdev);
-				return -EINVAL;
+				nope = -EINVAL;
+				break;
 			}
 			bdev = kmalloc_obj(*bdev);
-			if (!bdev)
-				return -ENOMEM;
+			if (!bdev) {
+				nope = -ENOMEM;
+				break;
+			}
 
 			bdev->dev = d;
 			bdev->minor = minor;
@@ -248,14 +253,23 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 				dev_err(dev->class_dev,
 					"Could not allocate memory. Out of memory?\n");
 				kfree(bdev);
-				return -ENOMEM;
+				nope = -ENOMEM;
+				break;
 			}
 			devpriv->devs = devs;
 			devpriv->devs[devpriv->ndevs++] = bdev;
+			nope = 0;	/* keeps the device open */
 
 			/* Append dev:subdev to devpriv->name */
 			seq_buf_printf(&s, "%u:%u ", bdev->minor, bdev->subdev);
 		}
+
+		if (nope) {
+			/* Opened device cannot be used, so close it. */
+			comedi_close_from(d, dev->minor);
+			if (nope < 0)
+				return nope;
+		}
 	}
 
 	if (!devpriv->nchans) {
Signature · 1 line
2.53.0
#1 lore raw
Quoted 10 lines · Ian Abbott

bonding_attach() calls do_dev_config() to open other comedi devices specified by the user and tries to make use of them as part of the 'bonding' device. If it successfully opens the other device but cannot use it (either because it does not any subdevices of a type the 'bonding' device can use, or because of some error that occurs while dealing with its subdevices) then it is left in the open state and the fact that it is open does not get recorded by the 'bonding' device, so the reference to the opened device gets lost. Fix it by closing the opened device if it could not be bound to the 'bonding' device.

Quoted 7 lines

To match previous behavior, if the opened device is unsuitable then ignore it (after closing it). The opened devices recorded by the 'bonding' device will eventually get closed by bonding_detach() even if bonding_attach() returns an error.

Fixes 3cf840f65ad5 ("Staging: comedi: add comedi_bond driver")

This is a known bot. I'll be glad to take this patch if you redo it from you and that you take the authorship. As-is, I can't take it from this fake "author", sorry.

greg k-h

author maintainer
#2 lore raw
Quoted 16 lines · Greg Kroah-Hartman
Quoted 15 lines · Ian Abbott

bonding_attach() calls do_dev_config() to open other comedi devices specified by the user and tries to make use of them as part of the 'bonding' device. If it successfully opens the other device but cannot use it (either because it does not any subdevices of a type the 'bonding' device can use, or because of some error that occurs while dealing with its subdevices) then it is left in the open state and the fact that it is open does not get recorded by the 'bonding' device, so the reference to the opened device gets lost. Fix it by closing the opened device if it could not be bound to the 'bonding' device.

To match previous behavior, if the opened device is unsuitable then ignore it (after closing it). The opened devices recorded by the 'bonding' device will eventually get closed by bonding_detach() even if bonding_attach() returns an error.

Quoted 8 lines
Quoted 2 lines
Fixes 3cf840f65ad5 ("Staging: comedi: add comedi_bond driver")

This is a known bot. I'll be glad to take this patch if you redo it from you and that you take the authorship. As-is, I can't take it from this fake "author", sorry.

greg k-h

Actually, I did redo it in a completely different way to the original patch. I just used the original patch as a bug report. Should I remove just the "Reported-by:" line, or should I also remove the "Link:" to the original patch from the bot?

Cheers,
Ian

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 24 lines · Ian Abbott
Quoted 23 lines · Greg Kroah-Hartman
Quoted 10 lines · Ian Abbott

bonding_attach() calls do_dev_config() to open other comedi devices specified by the user and tries to make use of them as part of the 'bonding' device. If it successfully opens the other device but cannot use it (either because it does not any subdevices of a type the 'bonding' device can use, or because of some error that occurs while dealing with its subdevices) then it is left in the open state and the fact that it is open does not get recorded by the 'bonding' device, so the reference to the opened device gets lost. Fix it by closing the opened device if it could not be bound to the 'bonding' device.

Quoted 7 lines

To match previous behavior, if the opened device is unsuitable then ignore it (after closing it). The opened devices recorded by the 'bonding' device will eventually get closed by bonding_detach() even if bonding_attach() returns an error.

Fixes 3cf840f65ad5 ("Staging: comedi: add comedi_bond driver")

This is a known bot. I'll be glad to take this patch if you redo it from you and that you take the authorship. As-is, I can't take it from this fake "author", sorry.

Quoted 6 lines
Quoted 1 line

greg k-h

Actually, I did redo it in a completely different way to the original patch. I just used the original patch as a bug report. Should I remove just the "Reported-by:" line, or should I also remove the "Link:" to the original patch from the bot?

Ah crap, I missed that this was reported-by and not the author. I'll go take this as-is, my fault, sorry for the noise.

greg k-h