mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bryan O'Donoghue <bod@kernel.org>
To: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
Cc: Dikshita Agarwal <dikshita.agarwal@oss.qualcomm.com>,
	Vikash Garodia <vikash.garodia@oss.qualcomm.com>,
	Abhinav Kumar <abhinav.kumar@linux.dev>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Vishnu Reddy <busanna.reddy@oss.qualcomm.com>,
	Hans Verkuil <hverkuil+cisco@kernel.org>,
	linux-media@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 3/3] media: iris: Add Gen2 firmware autodetect and fallback
Date: Sat, 6 Jun 2026 12:13:46 +0100	[thread overview]
Message-ID: <537d4aa4-789b-4962-ab0f-78602d2b5f1a@kernel.org> (raw)
In-Reply-To: <khlmf7hv4xcpe3zmcz3bfogexq53vlvur234d466jhggvbodpb@pk4yzijflfij>

On 06/06/2026 11:51, Dmitry Baryshkov wrote:
>> version = strnstr(data, marker, size);
> strnstr is defined to search for the substring in another string. There
> is no promise that it will work if data contains \0 chars (which would
> terminate the string).

I mean... we'd want to terminate on a NULL here I'd have thought. The 
subsequent strscpy, strncmp and sscanf in this routine would imply NULL 
termination.

No wait I see - the strscpy() in the original creates a putatively NULL 
terminated string from potentially non-NULL terminated data.


The key question is if this is a NULL terminated string or not. If not 
we would expect a header somewhere telling us the field length.

>> if (version) {
>> 	marker_off = version - data;
>> 	version += marker_len;
>> 	size -= marker_off + marker_len;
>>
>> 	if (version < terminator-3) {
>> 		/* This is safe because we bounds check */
>> 		if (strncmp("vfw", version, size) == 0)
>> 			return true;
>> 	}
>>
>> 	/* To do your sscanf() you need to create a zeorised buffer */
>> 	fat_buf = kzalloc(size+1, GFP_KERNEL);
>> 	if (!fat_buf)
>> 		return false;
>>
>> 	memcpy(fat_buf, version, size);
> Creating a copy of about the half of the image is definitely an
> overkill.

The image size part I wasn't sure about - were we dealing with a defined 
header or the _entire_ image with the given size.

That said - why are we scanning the entire image if size == sizeof(fw) 
anyway ?

There must be a maximum header size and if not a maximum we would be 
prepared to parse in-kernel - say the first 1k or 4k at max.

...
>> 	/* sscanf is now guaranteed to terminate on NULL */
>> 	if (sscanf(fat_buf, "video-firmware.%d.%d", &major, &minor) == 2) {
> I think we can replace this with string comparisons too. No need to
> sscanf it.
> 
> WDYT?

I'm just as happy with that. It was really this code "looked wrong" and 
so I dug into it a little bit.

The overflow is real. The size you pointed out is true. Take the 
suggested changes with a pinch of salt.

This is the part that really pinged me

+	for (size_t i = 0; i < max; i++) {
+		if (!memcmp(data + i, marker, mlen)) {

Iterating a string for a memcmp() instead of using standard string libs, 
only then when I looked did I see the overflow.

So long as that gets fixed I'm sanguine about the rest.

---
bod

  reply	other threads:[~2026-06-06 11:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-29 12:09 [PATCH v4 0/3] media: qcom: iris: Add generic Gen2 firmware detection and loading Dikshita Agarwal
2026-04-29 12:09 ` [PATCH v4 1/3] media: iris: Switch to hardware mode after firmware boot Dikshita Agarwal
2026-04-29 12:09 ` [PATCH v4 2/3] media: iris: Initialize HFI ops after firmware load in core init Dikshita Agarwal
2026-04-29 12:09 ` [PATCH v4 3/3] media: iris: Add Gen2 firmware autodetect and fallback Dikshita Agarwal
2026-05-07 19:04   ` Vikash Garodia
2026-05-13 15:07     ` Dmitry Baryshkov
2026-05-30 23:43   ` bod
2026-06-06 10:51     ` Dmitry Baryshkov
2026-06-06 11:13       ` Bryan O'Donoghue [this message]
2026-06-06 11:46         ` Dmitry Baryshkov

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=537d4aa4-789b-4962-ab0f-78602d2b5f1a@kernel.org \
    --to=bod@kernel.org \
    --cc=abhinav.kumar@linux.dev \
    --cc=busanna.reddy@oss.qualcomm.com \
    --cc=dikshita.agarwal@oss.qualcomm.com \
    --cc=dmitry.baryshkov@oss.qualcomm.com \
    --cc=hverkuil+cisco@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=vikash.garodia@oss.qualcomm.com \
    /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®