[PATCH v6] media: hdpvr: reject a second reader in hdpvr_read()
From: 25031212351
Date: Wed Nov 25 2026 - 08:16:35 EST
From: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
hdpvr_read() does not check dev->owner, although the rest of the driver
assumes a single active reader: vidioc_encoder_cmd() returns -EBUSY when
another file handle owns the stream, and hdpvr_release() only stops
streaming when the owner closes the file.
Two readers therefore consume the same buffers and update buf->pos
concurrently. When hdpvr_stop_streaming() has killed the URBs, the
buffers are put back on the free list with their old position, and the
worker resubmits them with urb->actual_length reset to 0. If the next
transfer completes with less data than the position a reader has already
consumed, urb->actual_length - buf->pos is negative. As rem is an int and
count is a size_t, "rem > count" is then true, so cnt becomes count and
copy_to_user() copies count bytes starting at transfer_buffer + buf->pos,
which can be past the end of the 8192 byte transfer buffer.
A KASAN slab-use-after-free read of 4096 bytes was observed in
hdpvr_read() with the source address 1281 bytes past the end of the
transfer buffer; the pages following the buffer had been recycled as slab
pages. A second report with a 42 byte copy was 25 bytes past the end of
the buffer.
Return -EBUSY when another file handle owns the stream, like
vidioc_encoder_cmd() does, and reset buf->pos when hdpvr_cancel_queue()
discards a buffer or when a new transfer is started, so that the position
always belongs to the current transfer. A stream restart can still race
with a read of the owning file handle, so start over at the beginning of
the buffer instead of letting the negative remainder become a full-size
copy.
Suggested-by: Hans Verkuil <hverkuil+cisco@xxxxxxxxxx>
Fixes: 9aba42efe85b ("V4L/DVB (11096): V4L2 Driver for the Hauppauge HD PVR usb capture device")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
v6:
- Reject a second reader with -EBUSY in hdpvr_read(), as
vidioc_encoder_cmd() already does; this is the root cause of the
concurrent buf->pos updates.
- Drop the generation counter and the buffer accounting helper. Only
reset buf->pos when a buffer is discarded or a new transfer is started,
and start over at the beginning of the buffer if the position no
longer matches the current transfer.
drivers/media/usb/hdpvr/hdpvr-video.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/drivers/media/usb/hdpvr/hdpvr-video.c b/drivers/media/usb/hdpvr/hdpvr-video.c
index 4290b2367fa3..620923acb4f1 100644
--- a/drivers/media/usb/hdpvr/hdpvr-video.c
+++ b/drivers/media/usb/hdpvr/hdpvr-video.c
@@ -92,6 +92,8 @@ int hdpvr_cancel_queue(struct hdpvr_device *dev)
list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
usb_kill_urb(buf->urb);
buf->status = BUFSTAT_AVAILABLE;
+ /* the data is discarded, the next transfer starts at 0 */
+ buf->pos = 0;
}
list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
@@ -210,6 +212,7 @@ static int hdpvr_submit_buffers(struct hdpvr_device *dev)
urb = buf->urb;
urb->status = 0;
urb->actual_length = 0;
+ buf->pos = 0;
ret = usb_submit_urb(urb, GFP_KERNEL);
if (ret) {
v4l2_err(&dev->v4l2_dev,
@@ -420,6 +423,10 @@ static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
return -ESPIPE;
mutex_lock(&dev->io_mutex);
+ if (dev->owner && file_to_v4l2_fh(file) != dev->owner) {
+ mutex_unlock(&dev->io_mutex);
+ return -EBUSY;
+ }
if (dev->status == STATUS_IDLE) {
if (hdpvr_start_streaming(dev)) {
v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
@@ -489,6 +496,14 @@ static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
/* set remaining bytes to copy */
urb = buf->urb;
rem = urb->actual_length - buf->pos;
+ if (rem < 0) {
+ /*
+ * A stream restart discarded the data this position
+ * belongs to: start over at the beginning.
+ */
+ buf->pos = 0;
+ continue;
+ }
cnt = rem > count ? count : rem;
if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
--
2.34.1