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 data on the device is most 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: # 2a0adbb80a2f: scsi: define depopulation capabilities related service actions Reviewed-by: Damien Le Moal Signed-off-by: Niklas Cassel --- Changes in v2: - Say that the data is most likely gone after REMOVE ELEMENT AND TRUNCATE (Damien). - Mark 2a0adbb80a2f, which added the SAI_* definitions, as a prerequisite for the stable backport (Damien). 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