All of lore.kernel.org
 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 2/2] media: s2255: advance marker pointer and use unaligned reads in save_frame
Date: Wed, 25 Nov 2026 22:21:02 +0000	[thread overview]
Message-ID: <20261125222102.1430675-3-elkin@google.com> (raw)
In-Reply-To: <20261125222102.1430675-1-elkin@google.com>

In save_frame(), pdword was initialized once from transfer_buffer before
the marker-search loop and never updated as jj incremented, repeatedly
inspecting offset 0 instead of scanning through the transfer buffer.

Derive pdata from pipe_info->transfer_buffer + jj on each iteration and
read the 32-bit little-endian marker and header fields via get_unaligned()
and get_unaligned_le32() since jj advances one byte at a time.

Fixes: 3fa0060526ea ("V4L/DVB: s2255drv: fixes for big endian arch")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---
 drivers/media/usb/s2255/s2255drv.c | 34 ++++++++++++++++--------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/drivers/media/usb/s2255/s2255drv.c b/drivers/media/usb/s2255/s2255drv.c
index 9ddc13c99a81..9b6979c811e2 100644
--- a/drivers/media/usb/s2255/s2255drv.c
+++ b/drivers/media/usb/s2255/s2255drv.c
@@ -31,6 +31,7 @@
 #include <linux/videodev2.h>
 #include <linux/mm.h>
 #include <linux/vmalloc.h>
+#include <linux/unaligned.h>
 #include <linux/usb.h>
 #include <media/videobuf2-v4l2.h>
 #include <media/videobuf2-vmalloc.h>
@@ -1716,23 +1717,24 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 	if (frm->state == S2255_READ_IDLE) {
 		u32 jj;
 		unsigned int cc;
-		__le32 *pdword; /*data from dsp is little endian */
-		int payload;
+		u32 payload;
 
 		if (pipe_info->cur_transfer_size < 20)
 			return -EINVAL;
 
 		/* search for marker codes */
-		pdata = pipe_info->transfer_buffer;
-		pdword = (__le32 *)pdata;
 		for (jj = 0; jj <= (pipe_info->cur_transfer_size - 20); jj++) {
-			switch (*pdword) {
+			__le32 marker, resp;
+
+			pdata = pipe_info->transfer_buffer + jj;
+			marker = get_unaligned((__le32 *)pdata);
+			switch (marker) {
 			case S2255_MARKER_FRAME:
 				dprintk(dev, 4, "marker @ offset: %u [%x %x]\n",
 					jj, pdata[0], pdata[1]);
 				offset = jj + PREFIX_SIZE;
 				bframe = 1;
-				cc = le32_to_cpu(pdword[1]);
+				cc = get_unaligned_le32(pdata + 4);
 				if (cc >= MAX_CHANNELS) {
 					dprintk(dev, 0,
 						"bad channel\n");
@@ -1741,7 +1743,7 @@ 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]);
+				payload = get_unaligned_le32(pdata + 12);
 				if (payload < PREFIX_SIZE ||
 				    payload > vc->req_image_size) {
 					vc->bad_payload++;
@@ -1749,7 +1751,7 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 					return -EINVAL;
 				}
 				vc->pkt_size = payload;
-				vc->jpg_size = le32_to_cpu(pdword[4]);
+				vc->jpg_size = get_unaligned_le32(pdata + 16);
 				break;
 			case S2255_MARKER_RESPONSE:
 				if (pipe_info->cur_transfer_size - jj - 20 <
@@ -1757,14 +1759,15 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 					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)
+				cc = get_unaligned_le32(pdata + 4);
+				if (cc >= MAX_CHANNELS)
 					break;
-				cc = G_chnmap[le32_to_cpu(pdword[1])];
+				cc = G_chnmap[cc];
 				if (cc >= MAX_CHANNELS)
 					break;
 				vc = &dev->vc[cc];
-				switch (pdword[2]) {
+				resp = get_unaligned((__le32 *)(pdata + 8));
+				switch (resp) {
 				case S2255_RESPONSE_SETMODE:
 					/* check if channel valid */
 					/* set mode ready */
@@ -1783,19 +1786,18 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
 					wake_up(&dev->fw_data->wait_fw);
 					break;
 				case S2255_RESPONSE_STATUS:
-					vc->vidstatus = le32_to_cpu(pdword[3]);
+					vc->vidstatus =
+						get_unaligned_le32(pdata + 12);
 					vc->vidstatus_ready = 1;
 					wake_up(&vc->wait_vidstatus);
 					dprintk(dev, 5, "vstat %x chan %d\n",
-						le32_to_cpu(pdword[3]), cc);
+						vc->vidstatus, cc);
 					break;
 				default:
 					pr_info("s2255 unknown resp\n");
 				}
-				pdata++;
 				break;
 			default:
-				pdata++;
 				break;
 			}
 			if (bframe)
-- 
2.56.1.839.g22a8290db9-goog


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

Thread overview: 4+ 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 in save_frame Elkin Cruz
2026-11-25 22:21 ` [PATCH 1/2] media: s2255: validate transfer size, payload, and offset " Elkin Cruz
2026-12-01  9:34   ` Hans Verkuil
2026-11-25 22:21 ` Elkin Cruz [this message]

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-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.