Re: [PATCH] scsi: NCR5380: Prevent potential out-of-bounds read in spi_print_msg()

From: Finn Thain
Date: Wed Apr 30 2025 - 23:40:44 EST



On Wed, 30 Apr 2025, Rand Deeb wrote:

> spi_print_msg() assumes that the input buffer is large enough to
> contain the full SCSI message, including extended messages which may
> access msg[2], msg[3], msg[7], and beyond based on message type.
>
> NCR5380_reselect() currently allocates a 3-byte buffer for 'msg'
> and reads only a single byte from the SCSI bus before passing it to
> spi_print_msg(), which can result in a potential out-of-bounds read
> if the message is malformed or declares a longer length.
>
> This patch increases the buffer size to 16 bytes and reads up to
> 16 bytes from the SCSI bus. A length check is also added to ensure
> the message is well-formed before passing it to spi_print_msg().
>
> This ensures safe handling of all valid SCSI messages and prevents
> undefined behavior due to malformed or malicious input.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>

I happen to agree with James that there is no value in trying to defend
against hostile SPI controllers, buses and targets. But I see a lot of
value in static checking so I'm not against removing theoretical issues
from the code if it makes static checking easier.

AFAIK the error path in question doesn't get executed in practice, like
James said. So you could drop the spi_print_msg() call in favour of this:

shost_printk(KERN_ERR, instance,
"expecting IDENTIFY message, got 0x%02x\n", msg[0]);

But it's not clear to me that you can sidestep the API issue that way. Do
the other callers of spi_print_msg() not have the same issue?