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 --- 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 #include +#include #include #include #include @@ -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