]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
comedi: comedi_bond: Check for loops when bonding devices
authorIan Abbott <abbotti@mev.co.uk>
Mon, 27 Oct 2025 15:25:03 +0000 (15:25 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 26 Nov 2025 13:20:10 +0000 (14:20 +0100)
The "comedi_bond" driver allows a composite COMEDI device to be built up
from the subdevices of other COMEDI devices, although it currently only
supports digital I/O subdevices.  Although it checks that it is not
trying to bind to itself, it is possible to end up with a cycle of
"comedi_bond" devices bound to each other.  For example:

1. Configure /dev/comedi0 to use some COMEDI hardware device with
   digital I/O subdevices, but not a "comedi_bond" device.
2. Configure /dev/comedi1 as a "comedi_bond" device bound to
   /dev/comedi0.
3. Unconfigure /dev/comedi0 and reconfigure it as a "comedi_bond" device
   bound to /dev/comedi1.

Now we have /dev/comedi0 and /dev/comedi1 bound in a cycle.  When an
operation is performed on the digital I/O subdevice of /dev/comedi0 for
example, it will try and perform the operation on /dev/comedi1, which
will try and perform the operation on /dev/comedi0.  The task will end
up deadlocked trying to lock /dev/comedi0's mutex which it has already
locked.

I discovered that possibility while investigating fix sysbot crash
https://syzkaller.appspot.com/bug?extid=4a6138c17a47937dcea1 ("possible
deadlock in comedi_do_insn"), but I think that report may be a false
positive.

To avoid that, replace the calls to `comedi_open()` and `comedi_close()`
in "kcomedilib" with calls to `comedi_open_from()` and
`comedi_close_from()`.  These take an extra parameter that indicates the
COMEDI minor device number from which the open or close is being
performed.  `comedi_open_from()` will refuse to open the device if doing
so would result in a cycle.  The cycle detection depends on the extra
parameter having the correct value for this device and also for existing
devices in the chain.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Link: https://patch.msgid.link/20251027153748.4569-3-abbotti@mev.co.uk
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/comedi/drivers/comedi_bond.c

index 78c39fa841776ff8ffd390a733a7929ca4b87967..30650fa36fff25b103bd8d16fe1140ac08e80b46 100644 (file)
@@ -205,7 +205,7 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
                snprintf(file, sizeof(file), "/dev/comedi%d", minor);
                file[sizeof(file) - 1] = 0;
 
-               d = comedi_open(file);
+               d = comedi_open_from(file, dev->minor);
 
                if (!d) {
                        dev_err(dev->class_dev,
@@ -326,7 +326,7 @@ static void bonding_detach(struct comedi_device *dev)
                        if (!bdev)
                                continue;
                        if (!test_and_set_bit(bdev->minor, devs_closed))
-                               comedi_close(bdev->dev);
+                               comedi_close_from(bdev->dev, dev->minor);
                        kfree(bdev);
                }
                kfree(devpriv->devs);