Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yang Xiuwei" <yangxiuwei@kylinos.cn>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v1 3/4] scsi: sd: fix special_vec mempool leak when scsi_alloc_sgtables() fails
Date: Tue, 23 Jun 2026 10:20:56 +0000	[thread overview]
Message-ID: <20260623102056.E11C11F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260623100159.4018066-4-yangxiuwei@kylinos.cn>

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

Pre-existing issues:
- [High] Concurrent updates to `sdp->sector_size` can cause `sd_uninit_command()` to free a payload page to the wrong mempool.
--

commit ba587f7eceae0f0ec53afb6d48daeb8d25456542
Author: Yang Xiuwei <yangxiuwei@kylinos.cn>

scsi: sd: fix special_vec mempool leak when scsi_alloc_sgtables() fails

sd_set_special_bvec() allocates a special payload page for UNMAP and
WRITE SAME commands.  If scsi_alloc_sgtables() fails afterward in
sd_setup_unmap_cmnd() or sd_setup_write_same{10,16}_cmnd(), the SCSI
midlayer does not call uninit_command() because RQF_DONTPREP is not
set yet, leaking the page.

Call sd_uninit_command() on error, and clear RQF_SPECIAL_PAYLOAD after
freeing the page.

> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index b096ea237f14a..6f05e7683df6d 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
> @@ -958,6 +958,21 @@ static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
>  	return protect;
>  }
>  
> +static void sd_uninit_command(struct scsi_cmnd *cmd)
> +{
> +	struct request *rq = scsi_cmd_to_rq(cmd);
> +	struct scsi_device *sdp = cmd->device;
> +
> +	if (!(rq->rq_flags & RQF_SPECIAL_PAYLOAD))
> +		return;
> +
> +	if (sdp->sector_size > PAGE_SIZE)
> +		mempool_free(rq->special_vec.bv_page, sd_large_page_pool);
> +	else
> +		mempool_free(rq->special_vec.bv_page, sd_page_pool);

[Severity: High]
This is a pre-existing issue, but can checking sdp->sector_size here cause a
page to be freed into the wrong mempool if the sector size changes
concurrently?

In sd_set_special_bvec(), the page is allocated based on sdp->sector_size:

	if (sector_size > PAGE_SIZE)
		page = mempool_alloc(sd_large_page_pool, GFP_ATOMIC);

Concurrently, sd_revalidate_disk() calls sd_read_capacity(), which updates
sdkp->device->sector_size directly before the block layer queue is frozen:

	sdkp->device->sector_size = sector_size;

If sdp->sector_size is modified and crosses the PAGE_SIZE boundary while an
UNMAP or WRITE SAME request is in flight, could sd_uninit_command() read the
new size and free the page to the wrong pool? This might be reachable by a
malicious USB device returning a different sector size upon a media change.
Since sd_large_page_pool uses high-order allocations, freeing an order-0 page
into it could corrupt the page allocator and cause subsequent allocations to
write out of bounds.

> +	rq->rq_flags &= ~RQF_SPECIAL_PAYLOAD;
> +}
> +

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260623100159.4018066-1-yangxiuwei@kylinos.cn?part=3

  reply	other threads:[~2026-06-23 10:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 10:01 [PATCH v1 0/4] scsi: sd: fix probe error cleanup, special_vec leak and sd_done() sense gate Yang Xiuwei
2026-06-23 10:01 ` [PATCH v1 1/4] scsi: sd: fix error handling in sd_probe() after large pool creation failure Yang Xiuwei
2026-06-26 21:52   ` Damien Le Moal
2026-06-23 10:01 ` [PATCH v1 2/4] scsi: sd: unify sd_probe() error cleanup through out_put Yang Xiuwei
2026-06-26 21:54   ` Damien Le Moal
2026-06-29  1:16     ` Yang Xiuwei
2026-06-29  5:03       ` Damien Le Moal
2026-06-23 10:01 ` [PATCH v1 3/4] scsi: sd: fix special_vec mempool leak when scsi_alloc_sgtables() fails Yang Xiuwei
2026-06-23 10:20   ` sashiko-bot [this message]
2026-06-26 21:57   ` Damien Le Moal
2026-06-23 10:01 ` [PATCH v1 4/4] scsi: sd: fix sd_done() sense handling condition Yang Xiuwei
2026-06-23 10:18   ` sashiko-bot
2026-06-26 22:07   ` Damien Le Moal

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=20260623102056.E11C11F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yangxiuwei@kylinos.cn \
    /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