[PATCH v3 5/5] scsi: sd: Fix truncation of WRITE SAME(10) requests that don't fit

From: Chris Down

Date: Sun Nov 29 2026 - 14:25:50 EST


The modes and the limits keep most requests that don't fit in WRITE
SAME(10) away from sd_setup_write_same10_cmnd(), but they can't stop
all of them. One that gets there is sent with its LBA or count
truncated.

For example, bio_check_eod() only checks a bio against its partition,
and it does that before the bio enters the queue. If a disk shrinks to
2^32 blocks or fewer while a partition on it still ends above that, a
discard to the end of the partition is sent as WRITE SAME(10) with its
LBA truncated. So is one that was checked before the shrink and entered
the queue after it.

The block layer also doesn't split a discard when max_discard_sectors
is zero or below the discard granularity. With scsi_debug, 512-byte
blocks and an unmap granularity of 0x10000 blocks, a 64 MiB discard in
writesame_10 mode goes out as a single WRITE SAME(10) with a count of
zero. scsi_debug doesn't unmap anything in that case, but in SBC a
count of zero is intended to imply that every block from the LBA to the
end of the disk should be unmapped unless the device sets WSNZ. Writing
0 to discard_max_bytes while a large discard is running sends the rest
of the bio unsplit as well.

sd_setup_read_write_cmnd() already fails reads and writes beyond the
end of the disk. Let's fail a request that doesn't fit in WRITE
SAME(10) too. The block layer still has to learn to split or refuse the
discards above, but that is a separate fix.

Fixes: c98a0eb0e90d ("[SCSI] sd: Logical Block Provisioning update")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
drivers/scsi/sd.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index f4db971d7abe..ab264a829930 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1188,6 +1188,9 @@ static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
u32 data_len = sdp->sector_size;
blk_status_t ret;

+ if (lba > U32_MAX || nr_blocks > U16_MAX)
+ return BLK_STS_TARGET;
+
if (!sd_set_special_bvec(cmd, data_len))
return BLK_STS_RESOURCE;

--
2.56.1