Netdev List
 help / color / mirror / Atom feed
From: Ivan Vecera <ivecera@redhat.com>
To: netdev@vger.kernel.org
Cc: Min Li <min.li@microchip.com>,
	Vadim Fedorenko <vadim.fedorenko@linux.dev>,
	Arkadiusz Kubalewski <arkadiusz.kubalewski@intel.com>,
	Jiri Pirko <jiri@resnulli.us>, Jakub Kicinski <kuba@kernel.org>,
	Przemek Kitszel <przemyslaw.kitszel@intel.com>
Subject: [PATCH net] dpll: zl3073x: fix parsing of the end of firmware file
Date: Fri,  4 Dec 2026 12:13:31 +0100	[thread overview]
Message-ID: <20261204111331.380897-1-ivecera@redhat.com> (raw)

zl3073x_fw_component_load() copies the input into a local buffer with
strscpy(buf, *psrc, min(sizeof(buf), *psize)). strscpy() needs room for
the NUL terminator within the given size, so the last byte of the
firmware file is never parsed. If the file does not end with
whitespace, the last data word loses its last hex digit and the wrong
value is written to the device flash without any error.

The end of input is also misdetected. It is recognized only when the
header sscanf() returns 0, but the kernel vsscanf() counts an empty %s
match on whitespace-only input. Longer runs of whitespace, e.g. 25 or
more bytes after the last data word, are then rejected as "invalid
component size".

Add a helper that skips whitespace in the input and copies the
following bytes including the last one, and use it for both the
component header and the data words.

Discovered by an AI code review agent (Sashiko), not observed with real
firmware files. Confirmed without kernel modifications by building the
parser from fw.c with the kernel vsscanf() as a userspace test: the
input "utility 1\nDEADBEEF" without a trailing newline loads the word
as 0x0DEADBEE, and 25 newlines after it fail the load with -EINVAL.
With the fix, the test parses both inputs and 300000 random files with
varying whitespace correctly, without ASan or UBSan reports. Not tested
on hardware.

Fixes: ca017409da69 ("dpll: zl3073x: Add firmware loading functionality")
Signed-off-by: Ivan Vecera <ivecera@redhat.com>
---
 drivers/dpll/zl3073x/fw.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/drivers/dpll/zl3073x/fw.c b/drivers/dpll/zl3073x/fw.c
index 3acbb33ed7b8d0..fb77f6811f25ad 100644
--- a/drivers/dpll/zl3073x/fw.c
+++ b/drivers/dpll/zl3073x/fw.c
@@ -2,6 +2,7 @@
 
 #include <linux/array_size.h>
 #include <linux/build_bug.h>
+#include <linux/ctype.h>
 #include <linux/dev_printk.h>
 #include <linux/err.h>
 #include <linux/errno.h>
@@ -176,6 +177,34 @@ zl3073x_fw_component_id_get(const char *name)
 	return ZL_FW_COMPONENT_INVALID;
 }
 
+/**
+ * zl3073x_fw_input_get - Get the beginning of the remaining input
+ * @buf: buffer to store the input to
+ * @len: size of @buf
+ * @psrc: data pointer to the remaining input
+ * @psize: remaining bytes in input
+ *
+ * The function skips whitespace in the input and copies up to @len - 1
+ * following bytes into @buf and NUL-terminates it. The input does not have
+ * to be NUL-terminated, so its last byte is copied as well. Source data
+ * pointer @psrc and remaining bytes @psize are moved past the skipped
+ * whitespace.
+ */
+static void
+zl3073x_fw_input_get(char *buf, size_t len, const char **psrc, size_t *psize)
+{
+	size_t n;
+
+	while (*psize && isspace(**psrc)) {
+		(*psrc)++;
+		(*psize)--;
+	}
+
+	n = min(len - 1, *psize);
+	memcpy(buf, *psrc, n);
+	buf[n] = '\0';
+}
+
 /**
  * zl3073x_fw_component_load - Load component from firmware source
  * @zldev: zl3073x device structure
@@ -209,7 +238,7 @@ zl3073x_fw_component_load(struct zl3073x_dev *zldev,
 	int pos, rc;
 
 	/* Fetch image name and size from input */
-	strscpy(buf, *psrc, min(sizeof(buf), *psize));
+	zl3073x_fw_input_get(buf, sizeof(buf), psrc, psize);
 	rc = sscanf(buf, "%15s %u %n", name, &count, &pos);
 	if (!rc) {
 		/* No more data */
@@ -252,7 +281,7 @@ zl3073x_fw_component_load(struct zl3073x_dev *zldev,
 
 	/* Load component data from firmware source */
 	for (dest = comp->data; count; count--, dest++) {
-		strscpy(buf, *psrc, min(sizeof(buf), *psize));
+		zl3073x_fw_input_get(buf, sizeof(buf), psrc, psize);
 		rc = sscanf(buf, "%x %n", dest, &pos);
 		if (!rc)
 			goto err_data;

base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
-- 
2.56.1


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

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261204111331.380897-1-ivecera@redhat.com \
    --to=ivecera@redhat.com \
    --cc=arkadiusz.kubalewski@intel.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=min.li@microchip.com \
    --cc=netdev@vger.kernel.org \
    --cc=przemyslaw.kitszel@intel.com \
    --cc=vadim.fedorenko@linux.dev \
    /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