Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jay Vadayath" <jay@artiphishell.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH] scsi: sd: validate device-supplied mode sense lengths in cache_type_store
Date: Fri, 17 Jul 2026 19:23:13 +0000	[thread overview]
Message-ID: <20260717192313.93D791F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260717191023.15011-1-jay@artiphishell.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Stack out-of-bounds read and write in `scsi_cdl_enable` due to unvalidated device-supplied mode sense lengths.
- [High] Data race on `struct scsi_disk` bitfields between `cache_type_store` and `sd_revalidate_disk`.
--

--- Patch [1]: [PATCH] scsi: sd: validate device-supplied mode sense lengths in cache_type_store ---
commit 58b82fd0109d95a6ff61d8f8d2790723466ad640
Author: Jay Vadayath <jay@artiphishell.com>

scsi: sd: validate device-supplied mode sense lengths in cache_type_store

This commit adds bounds checking to cache_type_store() to prevent an
out-of-bounds stack access when parsing device-supplied MODE SENSE lengths.
It explicitly verifies that the combined header and block descriptor
lengths do not exceed the on-stack buffer size before calculating offsets.

> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index 599e75f333343..4234ce314c9d9 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c

[ ... ]

> @@ -268,10 +268,19 @@ cache_type_store(struct device *dev, struct device_attribute *attr,
>  	if (scsi_mode_sense(sdp, 0x08, 8, 0, buffer, sizeof(buffer), SD_TIMEOUT,
>  			    sdkp->max_retries, &data, NULL))
>  		return -EINVAL;
> -	len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
> -		  data.block_descriptor_length);
> -	buffer_data = buffer + data.header_length +
> -		data.block_descriptor_length;
> +
> +	/*
> +	 * The mode parameter header and block descriptor lengths are
> +	 * supplied by the device and must not be trusted (e.g. a malicious
> +	 * USB mass storage device).  Reject responses that would place the
> +	 * caching mode page (of which we touch the first three bytes)
> +	 * outside of the buffer to avoid an out-of-bounds access below.
> +	 */
> +	offset = data.header_length + data.block_descriptor_length;
> +	if (offset + 3 > sizeof(buffer))
> +		return -EINVAL;

[Severity: High]
This is a pre-existing issue, but does modifying bitfields in cache_type_store()
create a data race with other device events?

Earlier in this same function, the code directly modifies sdkp->cache_override:

drivers/scsi/sd.c:cache_type_store() {
    ...
    if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
        buf += sizeof(temp) - 1;
        sdkp->cache_override = 1;
    ...
}

Concurrently, during a device rescan or hotplug event, sd_read_cache_type()
modifies adjacent bitfields in the same struct:

drivers/scsi/sd.c:sd_read_cache_type() {
    ...
    sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
    ...
}

Since bitfield updates are read-modify-write operations, could a concurrent
sysfs write to cache_type silently corrupt other bitfields like DPOFUA
or lbpme if they aren't protected by a shared lock?

[Severity: Critical]
This is a pre-existing issue, but does a structurally identical out-of-bounds
vulnerability exist in scsi_cdl_enable() in drivers/scsi/scsi.c?

Looking at scsi_cdl_enable(), the same device-supplied lengths are used to
calculate an offset into an on-stack buffer without bounds checking:

drivers/scsi/scsi.c:scsi_cdl_enable() {
    ...
    buf_data = buf + data.header_length + data.block_descriptor_length;
    ...
    buf_data[4] &= ~0x03;
    ...
}

If a malicious USB device returns a large block_descriptor_length, could
this calculation exceed the stack buffer bounds and lead to memory corruption,
just like the issue fixed here in cache_type_store()?

> +	len = min_t(size_t, sizeof(buffer) - offset, data.length - offset);
> +	buffer_data = buffer + offset;
>  	buffer_data[2] &= ~0x05;
>  	buffer_data[2] |= wce << 2 | rcd;
>  	sp = buffer_data[0] & 0x80 ? 1 : 0;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260717191023.15011-1-jay@artiphishell.com?part=1

  reply	other threads:[~2026-07-17 19:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-17 19:10 [PATCH] scsi: sd: validate device-supplied mode sense lengths in cache_type_store Jay Vadayath
2026-07-17 19:23 ` sashiko-bot [this message]
2026-07-17 19:24 ` James Bottomley

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=20260717192313.93D791F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jay@artiphishell.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.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