Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: qla2xxx: Format SFP EEPROM vendor strings with %.*s
@ 2026-11-19 17:05 Ian Rogers
  2026-11-19 17:12 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2026-11-19 17:05 UTC (permalink / raw)
  To: Nilesh Javali, GR-QLogic-Storage-Upstream, James E.J. Bottomley,
	Martin K. Petersen
  Cc: linux-scsi, linux-kernel, llvm, Elkin Cruz, Ian Rogers

In qla2xxx_print_sfp_info(), the verbose logging path passes the 16-byte
SFF-8472 transceiver EEPROM fields a0->vendor_name and a0->vendor_pn
directly as the format string argument to snprintf(). Per SFF-8472, these
ASCII fields are space-padded rather than NUL-terminated, and their bytes
originate from pluggable SFP optics over I2C; any '%' byte in EEPROM data
or a corrupted I2C read is interpreted as a printf format specifier with
no variadic arguments supplied.

Format a0->vendor_name and a0->vendor_pn directly in ql_dbg() using "%.*s"
bounded by sizeof(), matching the non-verbose ql_dbg() call immediately
above in the same function.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with
-Wformat-security enabled; fix and changelog drafted with an AI coding
assistant. Build-tested under LLVM=1 W=1 and -Wformat-security; not tested
on QLogic Fibre Channel hardware.

Fixes: e4e3a2ce9556 ("scsi: qla2xxx: Add ability to autodetect SFP type")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 drivers/scsi/qla2xxx/qla_init.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/qla2xxx/qla_init.c b/drivers/scsi/qla2xxx/qla_init.c
index ccb039d97353..77504cfc0741 100644
--- a/drivers/scsi/qla2xxx/qla_init.c
+++ b/drivers/scsi/qla2xxx/qla_init.c
@@ -4126,15 +4126,13 @@ static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
 	if (!(ql2xextended_error_logging & ql_dbg_verbose))
 		return;
 
-	memset(str, 0, STR_LEN);
-	snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
 	ql_dbg(ql_dbg_init, vha, 0x015a,
-	    "SFP MFG Name: %s\n", str);
+	    "SFP MFG Name: %.*s\n",
+	    (int)sizeof(a0->vendor_name), a0->vendor_name);
 
-	memset(str, 0, STR_LEN);
-	snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
 	ql_dbg(ql_dbg_init, vha, 0x015c,
-	    "SFP Part Name: %s\n", str);
+	    "SFP Part Name: %.*s\n",
+	    (int)sizeof(a0->vendor_pn), a0->vendor_pn);
 
 	/* media */
 	memset(str, 0, STR_LEN);
-- 
2.56.1.708.g77570a6a1d-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] scsi: qla2xxx: Format SFP EEPROM vendor strings with %.*s
  2026-11-19 17:05 [PATCH] scsi: qla2xxx: Format SFP EEPROM vendor strings with %.*s Ian Rogers
@ 2026-11-19 17:12 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-19 17:12 UTC (permalink / raw)
  To: Ian Rogers; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] scsi: qla2xxx: stack buffer overflow in
  qla2xxx_print_sfp_info() [1]
--

commit fc46a24e989dac1a4fa69f172af87de567da0070
Author: Ian Rogers <irogers@google.com>

scsi: qla2xxx: Format SFP EEPROM vendor strings with %.*s

This commit replaces snprintf() calls that passed SFP EEPROM vendor name and
part number fields directly as format strings in qla2xxx_print_sfp_info()
with "%.*s" formatting in ql_dbg().

> diff --git a/drivers/scsi/qla2xxx/qla_init.c b/drivers/scsi/qla2xxx/qla_init.c
> index ccb039d97353b..77504cfc07411 100644
> --- a/drivers/scsi/qla2xxx/qla_init.c
> +++ b/drivers/scsi/qla2xxx/qla_init.c
> @@ -4126,15 +4126,13 @@ static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
>  	if (!(ql2xextended_error_logging & ql_dbg_verbose))
>  		return;
>  
> -	memset(str, 0, STR_LEN);
> -	snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
>  	ql_dbg(ql_dbg_init, vha, 0x015a,
> -	    "SFP MFG Name: %s\n", str);
> +	    "SFP MFG Name: %.*s\n",
> +	    (int)sizeof(a0->vendor_name), a0->vendor_name);
>  
> -	memset(str, 0, STR_LEN);
> -	snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
>  	ql_dbg(ql_dbg_init, vha, 0x015c,
> -	    "SFP Part Name: %s\n", str);
> +	    "SFP Part Name: %.*s\n",
> +	    (int)sizeof(a0->vendor_pn), a0->vendor_pn);
>  
>  	/* media */
>  	memset(str, 0, STR_LEN);

[Severity: Critical]
This problem wasn't introduced by this patch, but can the media block
immediately below overflow str[] [1]?

drivers/scsi/qla2xxx/qla_init.c:
#define PRINT_FIELD(_field, _flag, _str) {		\
	if (a0->_field & _flag) {\
		if (p) {\
			strcat(ptr, "|");\
			ptr++;\
			leftover--;\
		} \
		len = snprintf(ptr, leftover, "%s", _str);	\
		p = 1;\
		leftover -= len;\
		ptr += len; \
	} \
}

drivers/scsi/qla2xxx/qla_init.c:qla2xxx_print_sfp_info() {
    ...
	/* media */
	memset(str, 0, STR_LEN);
	ptr = str;
	leftover = STR_LEN;
	p = len = 0;
	PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
	PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
	PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
	PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
	PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
	PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
	PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
    ...
}

str[] is 64 bytes (STR_LEN), while the seven media strings and their "|"
separators total 83 characters plus the NUL terminator.  Because snprintf()
returns the length that would have been written rather than the truncated
length, when all seven FC_MED_* bits are set in a0->fc_med_cc9, the sixth
PRINT_FIELD() drives leftover negative and advances ptr past the end of
str[], and the seventh writes out-of-bounds on the stack via strcat() and
triggers WARN_ON_ONCE() in vsnprintf().

[1] https://sashiko.dev/#/bug/linux-684c2982-ba32-44da-bc9c-910dc2c357ef

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119170511.1515433-1-irogers@google.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19 17:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 17:05 [PATCH] scsi: qla2xxx: Format SFP EEPROM vendor strings with %.*s Ian Rogers
2026-11-19 17:12 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox