media: usbvision: Fix races among open, close, and disconnect
authorAlan Stern <stern@rowland.harvard.edu>
Mon, 7 Oct 2019 15:09:53 +0000 (12:09 -0300)
committerMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Thu, 10 Oct 2019 10:22:38 +0000 (07:22 -0300)
commit9e08117c9d4efc1e1bc6fce83dab856d9fd284b6
tree2c0da72c72f2c6d06f5c3efb6354cc2fc7ba7eae
parentc7a191464078262bf799136317c95824e26a222b
media: usbvision: Fix races among open, close, and disconnect

Visual inspection of the usbvision driver shows that it suffers from
three races between its open, close, and disconnect handlers.  In
particular, the driver is careful to update its usbvision->user and
usbvision->remove_pending flags while holding the private mutex, but:

usbvision_v4l2_close() and usbvision_radio_close() don't hold
the mutex while they check the value of
usbvision->remove_pending;

usbvision_disconnect() doesn't hold the mutex while checking
the value of usbvision->user; and

also, usbvision_v4l2_open() and usbvision_radio_open() don't
check whether the device has been unplugged before allowing
the user to open the device files.

Each of these can potentially lead to usbvision_release() being called
twice and use-after-free errors.

This patch fixes the races by reading the flags while the mutex is
still held and checking for pending removes before allowing an open to
succeed.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: <stable@vger.kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
drivers/media/usb/usbvision/usbvision-video.c