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

From: 25031212351

Date: Fri Nov 20 2026 - 12:00:47 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 neither reset when
hdpvr_cancel_queue() recycles a buffer nor protected by dev->io_mutex.

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 using min_t() so that a negative
remainder cannot be turned into a full-size copy, and by accounting the
consumed bytes in hdpvr_account_buffer(), which recycles the buffer once it
has been read completely: the position, the buffer status and the buffer
lists are updated in the same critical section, and a buffer that has been
recycled and resubmitted in the meantime is left alone.

Reset buf->pos when hdpvr_cancel_queue() discards the buffers as well,
otherwise a restarted stream either skips the first bytes of the new
transfer or drops it entirely.

The buffer status is printed while dev->io_mutex is still held, as
print_buffer_status() walks both buffer lists.

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>
---
v4:
- Account the consumed bytes and recycle the buffer in the same critical
section, so a buffer that another reader or a stream restart has already
recycled and resubmitted is no longer modified after the fact.
- Print the buffer status while the buffer lists are still locked:
print_buffer_status() walks them and must not race with the worker.
- Merge the two patches of the v3 series back into one patch, as the
position, the status and the buffer lists form a single invariant.
drivers/media/usb/hdpvr/hdpvr-video.c | 75 ++++++++++++++++++++-------
1 file changed, 57 insertions(+), 18 deletions(-)

diff --git a/drivers/media/usb/hdpvr/hdpvr-video.c b/drivers/media/usb/hdpvr/hdpvr-video.c
index 4290b2367fa3..387ddde776ae 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);
@@ -246,6 +248,43 @@ static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
return buf;
}

+/*
+ * Account the bytes consumed from a buffer and hand it back to the worker
+ * once it has been read completely.
+ *
+ * The buffer may have been recycled and resubmitted, either by another
+ * reader or after a stream restart, while copy_to_user() slept. Its
+ * position and its place in the buffer lists then belong to the newer
+ * transfer and must not be touched, so only act if the buffer is still
+ * the one that was read from.
+ *
+ * Returns true if the buffer was handed back to the worker.
+ */
+static bool hdpvr_account_buffer(struct hdpvr_device *dev,
+ struct hdpvr_buffer *buf,
+ unsigned int pos, unsigned int new_pos)
+{
+ bool recycled = false;
+
+ mutex_lock(&dev->io_mutex);
+ if (buf->status == BUFSTAT_READY && buf->pos == pos) {
+ buf->pos = new_pos;
+ if (new_pos >= buf->urb->actual_length) {
+ buf->pos = 0;
+ buf->status = BUFSTAT_AVAILABLE;
+ list_move_tail(&buf->buff_list, &dev->free_buff_list);
+ print_buffer_status();
+ recycled = true;
+ }
+ }
+ mutex_unlock(&dev->io_mutex);
+
+ if (recycled)
+ wake_up_interruptible(&dev->wait_buffer);
+
+ return recycled;
+}
+
static void hdpvr_transmit_buffers(struct work_struct *work)
{
struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
@@ -415,6 +454,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 +528,23 @@ 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
+ * if it is still the buffer we were reading from.
+ */
+ hdpvr_account_buffer(dev, buf, buf_pos, buf_pos);
+
+ 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)
@@ -499,27 +552,13 @@ static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
goto err;
}

- buf->pos += cnt;
count -= cnt;
buffer += cnt;
ret += cnt;

/* finished, take next buffer */
- if (buf->pos == urb->actual_length) {
- mutex_lock(&dev->io_mutex);
- buf->pos = 0;
- buf->status = BUFSTAT_AVAILABLE;
-
- list_move_tail(&buf->buff_list, &dev->free_buff_list);
-
- print_buffer_status();
-
- mutex_unlock(&dev->io_mutex);
-
- wake_up_interruptible(&dev->wait_buffer);
-
+ if (hdpvr_account_buffer(dev, buf, buf_pos, buf_pos + cnt))
buf = hdpvr_get_next_buffer(dev);
- }
}
err:
if (!ret && !buf)
--
2.34.1