[PATCH] media: hdpvr: fix out-of-bounds read in hdpvr_read()

From: 25031212351

Date: Mon Nov 16 2026 - 12:55:01 EST


From: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>

hdpvr_read() computes the number of bytes left in the current buffer as
urb->actual_length - buf->pos and uses that as the length for
copy_to_user(). buf->pos is a plain unsigned int that is not reset when
hdpvr_cancel_queue() recycles a buffer, and it is not protected against
concurrent readers of the same device.

After 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, the
subtraction yields a negative value. As rem is an int and count is a
size_t, the comparison "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. Two readers of the same video device can corrupt each
other's position in the same way, and buf->pos is read twice, once for
rem and once for the source address.

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.

Fix this by reading the position once, by handing a buffer whose
position reached actual_length back to the worker, and by using min_t()
so that a negative remainder can no longer be turned into a full-size
copy.

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>
---
drivers/media/usb/hdpvr/hdpvr-video.c | 25 ++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/drivers/media/usb/hdpvr/hdpvr-video.c b/drivers/media/usb/hdpvr/hdpvr-video.c
index 4290b2367fa3..364a55dbdde8 100644
--- a/drivers/media/usb/hdpvr/hdpvr-video.c
+++ b/drivers/media/usb/hdpvr/hdpvr-video.c
@@ -415,6 +415,7 @@ static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
struct urb *urb;
int ret = 0;
int rem, cnt;
+ unsigned int buf_pos;

if (*pos)
return -ESPIPE;
@@ -488,10 +489,28 @@ 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;
- cnt = rem > count ? count : rem;
+ buf_pos = buf->pos;
+ rem = urb->actual_length - buf_pos;
+ if (rem <= 0) {
+ /*
+ * The buffer was recycled and refilled with less data
+ * than this reader already consumed from it, so there
+ * is nothing left to copy. Give it back to the worker.
+ */
+ mutex_lock(&dev->io_mutex);
+ buf->pos = 0;
+ buf->status = BUFSTAT_AVAILABLE;
+ list_move_tail(&buf->buff_list, &dev->free_buff_list);
+ mutex_unlock(&dev->io_mutex);
+
+ wake_up_interruptible(&dev->wait_buffer);
+
+ buf = hdpvr_get_next_buffer(dev);
+ continue;
+ }
+ cnt = min_t(int, rem, count);

- if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
+ if (copy_to_user(buffer, urb->transfer_buffer + buf_pos,
cnt)) {
v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
if (!ret)
--
2.34.1