Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] scsi: scsi_ioctl: require write access for the depopulation commands
@ 2026-11-16 18:31 Niklas Cassel
  2026-11-16 20:40 ` Damien Le Moal
  0 siblings, 1 reply; 3+ messages in thread
From: Niklas Cassel @ 2026-11-16 18:31 UTC (permalink / raw)
  To: James E.J. Bottomley, Martin K. Petersen
  Cc: Damien Le Moal, Niklas Cassel, stable, linux-scsi

scsi_cmd_allowed() lets anybody who can open a SCSI device, even
read-only, send any SERVICE ACTION IN(16) command. Besides READ
CAPACITY(16) and the other read-only service actions, that opcode also
carries the storage element depopulation commands of SBC-4: REMOVE
ELEMENT AND TRUNCATE, RESTORE ELEMENTS AND REBUILD and REMOVE ELEMENT
AND MODIFY ZONES. These change the capacity of the device, and after
REMOVE ELEMENT AND TRUNCATE the contents of the user data area may have
no relation to what they were before.

SCSI devices that support depopulation execute these commands directly,
and libata translates them to the equivalent ATA commands, so a user
with read-only access to the device node can truncate the device through
SG_IO, SCSI_IOCTL_SEND_COMMAND or bsg.

Treat these service actions like FORMAT UNIT and ERASE, which also
destroy data, and only allow them if the device is opened for writing.
The other SERVICE ACTION IN(16) service actions remain allowed for
everybody who can open the device.

Cc: stable@vger.kernel.org
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
 drivers/scsi/scsi_ioctl.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index bf049d9cb719..91525103d074 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -300,7 +300,6 @@ bool scsi_cmd_allowed(unsigned char *cmd, bool open_for_write)
 	case GPCMD_VERIFY_10:
 	case VERIFY_16:
 	case REPORT_LUNS:
-	case SERVICE_ACTION_IN_16:
 	case RECEIVE_DIAGNOSTIC:
 	case MAINTENANCE_IN: /* also GPCMD_SEND_KEY, which is a write command */
 	case GPCMD_READ_BUFFER_CAPACITY:
@@ -330,6 +329,21 @@ bool scsi_cmd_allowed(unsigned char *cmd, bool open_for_write)
 	/* ZBC */
 	case ZBC_IN:
 		return true;
+	/*
+	 * The storage element depopulation commands share the opcode of the
+	 * read-only SERVICE ACTION IN(16) commands, but change the capacity
+	 * of the device and may destroy its data, so treat them as writing
+	 * commands.
+	 */
+	case SERVICE_ACTION_IN_16:
+		switch (cmd[1] & 0x1f) {
+		case SAI_REMOVE_ELEMENT_AND_TRUNCATE:
+		case SAI_RESTORE_ELEMENTS_AND_REBUILD:
+		case SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES:
+			return open_for_write;
+		default:
+			return true;
+		}
 	/* Basic writing commands */
 	case WRITE_6:
 	case WRITE_10:
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: scsi_ioctl: require write access for the depopulation commands
  2026-11-16 18:31 [PATCH] scsi: scsi_ioctl: require write access for the depopulation commands Niklas Cassel
@ 2026-11-16 20:40 ` Damien Le Moal
  2026-11-17 12:26   ` Niklas Cassel
  0 siblings, 1 reply; 3+ messages in thread
From: Damien Le Moal @ 2026-11-16 20:40 UTC (permalink / raw)
  To: Niklas Cassel, James E.J. Bottomley, Martin K. Petersen
  Cc: stable, linux-scsi

On 11/16/26 10:31, Niklas Cassel wrote:
> scsi_cmd_allowed() lets anybody who can open a SCSI device, even
> read-only, send any SERVICE ACTION IN(16) command. Besides READ
> CAPACITY(16) and the other read-only service actions, that opcode also
> carries the storage element depopulation commands of SBC-4: REMOVE
> ELEMENT AND TRUNCATE, RESTORE ELEMENTS AND REBUILD and REMOVE ELEMENT
> AND MODIFY ZONES. These change the capacity of the device, and after
> REMOVE ELEMENT AND TRUNCATE the contents of the user data area may have
> no relation to what they were before.

Since that is basically reformatting the drive, I thing we can say that the
content is likely gone...

> SCSI devices that support depopulation execute these commands directly,
> and libata translates them to the equivalent ATA commands, so a user
> with read-only access to the device node can truncate the device through
> SG_IO, SCSI_IOCTL_SEND_COMMAND or bsg.
> 
> Treat these service actions like FORMAT UNIT and ERASE, which also
> destroy data, and only allow them if the device is opened for writing.
> The other SERVICE ACTION IN(16) service actions remain allowed for
> everybody who can open the device.
> 
> Cc: stable@vger.kernel.org

Hmmm... SAI_REMOVE_ELEMENT_AND_TRUNCATE and friends was defined with
2a0adbb80a2f ("scsi: define depopulation capabilities related service actions"),
which is in 7.3 only.

So maybe make that

Cc: stable@vger.kernel.org #7.3

?

Or maybe simply add:

Fixes: db496721cb0d ("ata: libata-scsi: add support for the REMOVE ELEMENT AND
TRUNCATE command")
Fixes: 1e307ca61a9c ("ata: libata-scsi: add support for the RESTORE ELEMENTS AND
REBUILD command")
Fixes: b448bb42bd10 ("ata: libata-scsi: add support for the REMOVE ELEMENT AND
MODIFY ZONES command")

> Signed-off-by: Niklas Cassel <cassel@kernel.org>

Otherwise looks good.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: scsi_ioctl: require write access for the depopulation commands
  2026-11-16 20:40 ` Damien Le Moal
@ 2026-11-17 12:26   ` Niklas Cassel
  0 siblings, 0 replies; 3+ messages in thread
From: Niklas Cassel @ 2026-11-17 12:26 UTC (permalink / raw)
  To: Damien Le Moal
  Cc: James E.J. Bottomley, Martin K. Petersen, stable, linux-scsi

On Mon, Nov 16, 2026 at 12:40:36PM -0800, Damien Le Moal wrote:
> On 11/16/26 10:31, Niklas Cassel wrote:
> > scsi_cmd_allowed() lets anybody who can open a SCSI device, even
> > read-only, send any SERVICE ACTION IN(16) command. Besides READ
> > CAPACITY(16) and the other read-only service actions, that opcode also
> > carries the storage element depopulation commands of SBC-4: REMOVE
> > ELEMENT AND TRUNCATE, RESTORE ELEMENTS AND REBUILD and REMOVE ELEMENT
> > AND MODIFY ZONES. These change the capacity of the device, and after
> > REMOVE ELEMENT AND TRUNCATE the contents of the user data area may have
> > no relation to what they were before.
> 
> Since that is basically reformatting the drive, I thing we can say that the
> content is likely gone...
> 
> > SCSI devices that support depopulation execute these commands directly,
> > and libata translates them to the equivalent ATA commands, so a user
> > with read-only access to the device node can truncate the device through
> > SG_IO, SCSI_IOCTL_SEND_COMMAND or bsg.
> > 
> > Treat these service actions like FORMAT UNIT and ERASE, which also
> > destroy data, and only allow them if the device is opened for writing.
> > The other SERVICE ACTION IN(16) service actions remain allowed for
> > everybody who can open the device.
> > 
> > Cc: stable@vger.kernel.org
> 
> Hmmm... SAI_REMOVE_ELEMENT_AND_TRUNCATE and friends was defined with
> 2a0adbb80a2f ("scsi: define depopulation capabilities related service actions"),
> which is in 7.3 only.
> 
> So maybe make that
> 
> Cc: stable@vger.kernel.org #7.3
> 
> ?

I will add:

Cc: <stable@vger.kernel.org> # 2a0adbb80a2f: scsi: define depopulation capabilities related service actions

to show the dependency as per Documentation/process/stable-kernel-rules.rst

because, while for SATA devices, this problem only exists since v7.3,
for SAS, this problem is much older.

REMOVE ELEMENT AND TRUNCATE was introduced in SBC-4 (r10) - released on
January 21, 2016.

REMOVE ELEMENT AND MODIFY ZONES command was introduced in ZBC-2 (r07),
around 2020.


Kind regards,
Niklas

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-17 12:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 18:31 [PATCH] scsi: scsi_ioctl: require write access for the depopulation commands Niklas Cassel
2026-11-16 20:40 ` Damien Le Moal
2026-11-17 12:26   ` Niklas Cassel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox