Skip to content

Commit 35b6fc5

Browse files
ian-abbottgregkh
authored andcommitted
comedi: fix race between polling and detaching
syzbot reports a use-after-free in comedi in the below link, which is due to comedi gladly removing the allocated async area even though poll requests are still active on the wait_queue_head inside of it. This can cause a use-after-free when the poll entries are later triggered or removed, as the memory for the wait_queue_head has been freed. We need to check there are no tasks queued on any of the subdevices' wait queues before allowing the device to be detached by the `COMEDI_DEVCONFIG` ioctl. Tasks will read-lock `dev->attach_lock` before adding themselves to the subdevice wait queue, so fix the problem in the `COMEDI_DEVCONFIG` ioctl handler by write-locking `dev->attach_lock` before checking that all of the subdevices are safe to be deleted. This includes testing for any sleepers on the subdevices' wait queues. It remains locked until the device has been detached. This requires the `comedi_device_detach()` function to be refactored slightly, moving the bulk of it into new function `comedi_device_detach_locked()`. Note that the refactor of `comedi_device_detach()` results in `comedi_device_cancel_all()` now being called while `dev->attach_lock` is write-locked, which wasn't the case previously, but that does not matter. Thanks to Jens Axboe for diagnosing the problem and co-developing this patch. Cc: stable <[email protected]> Fixes: 2f3fdcd ("staging: comedi: add rw_semaphore to protect against device detachment") Link: https://lore.kernel.org/all/[email protected]/ Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=01523a0ae5600aef5895 Co-developed-by: Jens Axboe <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Ian Abbott <[email protected]> Tested-by: Jens Axboe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 631ae0c commit 35b6fc5

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

drivers/comedi/comedi_fops.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ static int is_device_busy(struct comedi_device *dev)
787787
struct comedi_subdevice *s;
788788
int i;
789789

790+
lockdep_assert_held_write(&dev->attach_lock);
790791
lockdep_assert_held(&dev->mutex);
791792
if (!dev->attached)
792793
return 0;
@@ -795,7 +796,16 @@ static int is_device_busy(struct comedi_device *dev)
795796
s = &dev->subdevices[i];
796797
if (s->busy)
797798
return 1;
798-
if (s->async && comedi_buf_is_mmapped(s))
799+
if (!s->async)
800+
continue;
801+
if (comedi_buf_is_mmapped(s))
802+
return 1;
803+
/*
804+
* There may be tasks still waiting on the subdevice's wait
805+
* queue, although they should already be about to be removed
806+
* from it since the subdevice has no active async command.
807+
*/
808+
if (wq_has_sleeper(&s->async->wait_head))
799809
return 1;
800810
}
801811

@@ -825,15 +835,22 @@ static int do_devconfig_ioctl(struct comedi_device *dev,
825835
return -EPERM;
826836

827837
if (!arg) {
828-
if (is_device_busy(dev))
829-
return -EBUSY;
838+
int rc = 0;
839+
830840
if (dev->attached) {
831-
struct module *driver_module = dev->driver->module;
841+
down_write(&dev->attach_lock);
842+
if (is_device_busy(dev)) {
843+
rc = -EBUSY;
844+
} else {
845+
struct module *driver_module =
846+
dev->driver->module;
832847

833-
comedi_device_detach(dev);
834-
module_put(driver_module);
848+
comedi_device_detach_locked(dev);
849+
module_put(driver_module);
850+
}
851+
up_write(&dev->attach_lock);
835852
}
836-
return 0;
853+
return rc;
837854
}
838855

839856
if (copy_from_user(&it, arg, sizeof(it)))

drivers/comedi/comedi_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern struct mutex comedi_drivers_list_lock;
5050
int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
5151
struct comedi_insn *insn, unsigned int *data);
5252

53+
void comedi_device_detach_locked(struct comedi_device *dev);
5354
void comedi_device_detach(struct comedi_device *dev);
5455
int comedi_device_attach(struct comedi_device *dev,
5556
struct comedi_devconfig *it);

drivers/comedi/drivers.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static void comedi_device_detach_cleanup(struct comedi_device *dev)
158158
int i;
159159
struct comedi_subdevice *s;
160160

161-
lockdep_assert_held(&dev->attach_lock);
161+
lockdep_assert_held_write(&dev->attach_lock);
162162
lockdep_assert_held(&dev->mutex);
163163
if (dev->subdevices) {
164164
for (i = 0; i < dev->n_subdevices; i++) {
@@ -196,16 +196,23 @@ static void comedi_device_detach_cleanup(struct comedi_device *dev)
196196
comedi_clear_hw_dev(dev);
197197
}
198198

199-
void comedi_device_detach(struct comedi_device *dev)
199+
void comedi_device_detach_locked(struct comedi_device *dev)
200200
{
201+
lockdep_assert_held_write(&dev->attach_lock);
201202
lockdep_assert_held(&dev->mutex);
202203
comedi_device_cancel_all(dev);
203-
down_write(&dev->attach_lock);
204204
dev->attached = false;
205205
dev->detach_count++;
206206
if (dev->driver)
207207
dev->driver->detach(dev);
208208
comedi_device_detach_cleanup(dev);
209+
}
210+
211+
void comedi_device_detach(struct comedi_device *dev)
212+
{
213+
lockdep_assert_held(&dev->mutex);
214+
down_write(&dev->attach_lock);
215+
comedi_device_detach_locked(dev);
209216
up_write(&dev->attach_lock);
210217
}
211218

0 commit comments

Comments
 (0)