mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Elkin Cruz <elkin@google.com>
To: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	 llvm@lists.linux.dev, irogers@google.com,
	Elkin Cruz <elkin@google.com>,
	 stable@vger.kernel.org
Subject: [PATCH 1/2] media: s2255: validate transfer size, payload, and offset in save_frame
Date: Wed, 25 Nov 2026 22:21:01 +0000	[thread overview]
Message-ID: <20261125222102.1430675-2-elkin@google.com> (raw)
In-Reply-To: <20261125222102.1430675-1-elkin@google.com>

In read_pipe_completion(), pipe_info->cur_transfer_size is never
updated from purb->actual_length, so short USB bulk transfers leave
save_frame() scanning and copying up to 16384 bytes of stale data.
In save_frame(), marker scanning in S2255_READ_IDLE reads 20 bytes
(up to pdword[4]), but the loop bound only subtracts 12 from
cur_transfer_size without first checking cur_transfer_size >= 20.
In addition, skipping by DEF_USB_BLOCK can advance past the buffer,
payloads smaller than PREFIX_SIZE underflow vc->pkt_size - PREFIX_SIZE,
and frame offsets past cur_transfer_size underflow copy_size.

Record purb->actual_length (bounded by max_transfer_size) in
pipe_info->cur_transfer_size on completion while submitting URBs with
max_transfer_size, require cur_transfer_size >= 20, bound the loop by
cur_transfer_size - 20, cap DEF_USB_BLOCK skips against remaining
bytes, reject payloads smaller than PREFIX_SIZE, and validate offset
and frm->lpvbits before transitioning frm->state to S2255_READ_FRAME.

Fixes: 38f993ad8b1f ("V4L/DVB (8125): This driver adds support for the Sensoray 2255 devices.")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---
 drivers/media/usb/s2255/s2255drv.c | 51 ++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 16 deletions(-)

diff --git a/drivers/media/usb/s2255/s2255drv.c b/drivers/media/usb/s2255/s2255drv.c
index ba717cdf31ad..9ddc13c99a81 100644
--- a/drivers/media/usb/s2255/s2255drv.c
+++ b/drivers/media/usb/s2255/s2255drv.c
@@ -1714,17 +1714,21 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 	idx = vc->cur_frame;
 	frm = &vc->buffer.frame[idx];
 	if (frm->state == S2255_READ_IDLE) {
-		int jj;
+		u32 jj;
 		unsigned int cc;
 		__le32 *pdword; /*data from dsp is little endian */
 		int payload;
+
+		if (pipe_info->cur_transfer_size < 20)
+			return -EINVAL;
+
 		/* search for marker codes */
-		pdata = (unsigned char *)pipe_info->transfer_buffer;
+		pdata = pipe_info->transfer_buffer;
 		pdword = (__le32 *)pdata;
-		for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
+		for (jj = 0; jj <= (pipe_info->cur_transfer_size - 20); jj++) {
 			switch (*pdword) {
 			case S2255_MARKER_FRAME:
-				dprintk(dev, 4, "marker @ offset: %d [%x %x]\n",
+				dprintk(dev, 4, "marker @ offset: %u [%x %x]\n",
 					jj, pdata[0], pdata[1]);
 				offset = jj + PREFIX_SIZE;
 				bframe = 1;
@@ -1737,8 +1741,9 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 				/* reverse it */
 				dev->cc = G_chnmap[cc];
 				vc = &dev->vc[dev->cc];
-				payload =  le32_to_cpu(pdword[3]);
-				if (payload > vc->req_image_size) {
+				payload = le32_to_cpu(pdword[3]);
+				if (payload < PREFIX_SIZE ||
+				    payload > vc->req_image_size) {
 					vc->bad_payload++;
 					/* discard the bad frame */
 					return -EINVAL;
@@ -1747,9 +1752,12 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 				vc->jpg_size = le32_to_cpu(pdword[4]);
 				break;
 			case S2255_MARKER_RESPONSE:
-
-				pdata += DEF_USB_BLOCK;
-				jj += DEF_USB_BLOCK;
+				if (pipe_info->cur_transfer_size - jj - 20 <
+				    DEF_USB_BLOCK)
+					jj = pipe_info->cur_transfer_size - 20;
+				else
+					jj += DEF_USB_BLOCK;
+				pdata = pipe_info->transfer_buffer + jj;
 				if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS)
 					break;
 				cc = G_chnmap[le32_to_cpu(pdword[1])];
@@ -1806,20 +1814,28 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 		return -EINVAL;
 	}
 
-	if (frm->state == S2255_READ_IDLE) {
-		frm->state = S2255_READ_FRAME;
+	if (offset > pipe_info->cur_transfer_size) {
+		frm->state = S2255_READ_IDLE;
 		frm->cur_size = 0;
+		return -EINVAL;
 	}
 
-	/* skip the marker 512 bytes (and offset if out of sync) */
-	psrc = (u8 *)pipe_info->transfer_buffer + offset;
-
 	if (!frm->lpvbits) {
 		dprintk(dev, 1, "s2255 frame buffer == NULL.%p %p %d %d",
 			frm, dev, dev->cc, idx);
+		frm->state = S2255_READ_IDLE;
+		frm->cur_size = 0;
 		return -ENOMEM;
 	}
 
+	if (frm->state == S2255_READ_IDLE) {
+		frm->state = S2255_READ_FRAME;
+		frm->cur_size = 0;
+	}
+
+	/* skip the marker 512 bytes (and offset if out of sync) */
+	psrc = pipe_info->transfer_buffer + offset;
+
 	pdest = frm->lpvbits + frm->cur_size;
 
 	copy_size = (pipe_info->cur_transfer_size - offset);
@@ -2072,6 +2088,9 @@ static void read_pipe_completion(struct urb *purb)
 	}
 
 	if (status == 0) {
+		pipe_info->cur_transfer_size =
+			min_t(u32, purb->actual_length,
+			      pipe_info->max_transfer_size);
 		s2255_read_video_callback(dev, pipe_info);
 	} else {
 		pipe_info->err_count++;
@@ -2083,7 +2102,7 @@ static void read_pipe_completion(struct urb *purb)
 	usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
 			  pipe,
 			  pipe_info->transfer_buffer,
-			  pipe_info->cur_transfer_size,
+			  pipe_info->max_transfer_size,
 			  read_pipe_completion, pipe_info);
 
 	if (pipe_info->state != 0) {
@@ -2111,7 +2130,7 @@ static int s2255_start_readpipe(struct s2255_dev *dev)
 	usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
 			  pipe,
 			  pipe_info->transfer_buffer,
-			  pipe_info->cur_transfer_size,
+			  pipe_info->max_transfer_size,
 			  read_pipe_completion, pipe_info);
 	retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
 	if (retval) {
-- 
2.56.1.839.g22a8290db9-goog


  reply	other threads:[~2026-11-25 22:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 22:21 [PATCH 0/2] media: s2255: fix marker scan and bounds checks " Elkin Cruz
2026-11-25 22:21 ` Elkin Cruz [this message]
2026-11-25 22:21 ` [PATCH 2/2] media: s2255: advance marker pointer and use unaligned reads " Elkin Cruz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125222102.1430675-2-elkin@google.com \
    --to=elkin@google.com \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mchehab@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®