[PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes

From: Chris Down

Date: Sat Nov 28 2026 - 04:26:46 EST


In writesame_10_unmap mode we send write zeroes as WRITE SAME(10).
If the LBA or count doesn't fit, we truncate it and can return
success without zeroing the requested range. If the LBA wraps, we
can also corrupt live data near the start of the disk.

The LBA doesn't always fit because WRITE SAME(10) only has 32 bits for
it. Given that, we should only select writesame_10_unmap when a 32-bit
LBA can address the whole disk, and we shouldn't accept it in
zeroing_mode for a bigger disk either. A bigger disk gets WRITE SAME
without UNMAP instead, which already uses the 16-byte command where it
has to.

For the count, let's cap the queue limit when sd selects this mode so
large requests can still unmap in chunks. Writing to zeroing_mode
doesn't update the limits, so don't accept writesame_10_unmap there if
max_write_same_blocks is too big for it.

Fixes: e4b878371b85 ("sd: implement unmapping Write Zeroes")
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
drivers/scsi/sd.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index c7018a5de847..88b1c1d56d2f 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -146,6 +146,12 @@ static void sd_large_pool_destroy(void)
mutex_unlock(&sd_mutex_lock);
}

+/* WRITE SAME(10) only has a 32-bit LBA field. */
+static bool sd_can_use_ws10(struct scsi_disk *sdkp)
+{
+ return sdkp->capacity <= 1 + (sector_t)0xffffffff;
+}
+
static void sd_disable_discard(struct scsi_disk *sdkp)
{
sdkp->provisioning_mode = SD_LBP_DISABLE;
@@ -630,6 +636,11 @@ zeroing_mode_store(struct device *dev, struct device_attribute *attr,
if (mode < 0)
return -EINVAL;

+ if (mode == SD_ZERO_WS10_UNMAP &&
+ (!sd_can_use_ws10(sdkp) ||
+ sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS))
+ return -EINVAL;
+
sdkp->zeroing_mode = mode;

return count;
@@ -1214,13 +1225,18 @@ static void sd_config_write_same(struct scsi_disk *sdkp,

if (sdkp->lbprz && sdkp->lbpws)
sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP;
- else if (sdkp->lbprz && sdkp->lbpws10)
+ else if (sdkp->lbprz && sdkp->lbpws10 && sd_can_use_ws10(sdkp))
sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP;
else if (sdkp->max_ws_blocks)
sdkp->zeroing_mode = SD_ZERO_WS;
else
sdkp->zeroing_mode = SD_ZERO_WRITE;

+ /* Split large requests so they can still unmap. */
+ if (sdkp->zeroing_mode == SD_ZERO_WS10_UNMAP)
+ sdkp->max_ws_blocks = min(sdkp->max_ws_blocks,
+ (u32)SD_MAX_WS10_BLOCKS);
+
if (sdkp->max_ws_blocks &&
sdkp->physical_block_size > logical_block_size) {
/*
--
2.56.1