iscsi: Refuse to open as writable if the LUN is write protected
PATCH v2 1/1 iscsi: Refuse to open as writable if the LUN is write protected
Before, when a write protected iSCSI target is attached as scsi-disk with BDRV_O_RDWR, we report it as writable, while in fact all writes will fail.
One way to improve this is to report write protect flag as true to guest, but a even better way is to refuse using a write protected LUN to guest.
Target write protect flag is checked with a mode sense query.
---
v2: Improve error message.
Fall back to a warning if mode sense failed.
Check unmarshal return value.
--- block/iscsi.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
block/iscsi.c +46 -0
@@ -1219,6 +1219,44 @@ static void iscsi_attach_aio_context(BlockDriverState *bs, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); } +static bool iscsi_is_write_protected (IscsiLun *iscsilun) +{ +struct scsi_task *task; +struct scsi_mode_sense *ms =NULL ; + + task =iscsi_modesense6_sync (iscsilun->iscsi, iscsilun->lun, +1 ,SCSI_MODESENSE_PC_CURRENT , +0x3F , +0 ,255 ); + +if (task ==NULL ) { +error_report ("Failed to send MODE_SENSE6 command: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + +if (task->status !=SCSI_STATUS_GOOD ) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + ms =scsi_datain_unmarshall (task); +if (!ms) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } +out: +if (task) { +scsi_free_scsi_task (task); + } +if (!ms) { +error_report ("Assuming write enabled" ); +return false; + } +return ms->device_specific_parameter &0x80 ; +} +/* * We support iscsi url's on the form * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
@@ -1339,6 +1377,14 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,scsi_free_scsi_task (task); task =NULL ; +/* Check the write protect flag of the LUN if we want to write */ +if ((flags &BDRV_O_RDWR ) + &&iscsi_is_write_protected (iscsilun)) { +error_setg (errp,"Cannot open a write protected LUN as read-write" ); + ret = -EPERM ; + goto out; + } +iscsi_readcapacity_sync (iscsilun, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Signature · 1 line
1.9.3
Quoted 49 lines · Fam Zheng
Before, when a write protected iSCSI target is attached as scsi-disk with
BDRV_O_RDWR, we report it as writable, while in fact all writes will fail.One way to improve this is to report write protect flag as true to guest, but a even better way is to refuse using a write protected LUN to guest.
Target write protect flag is checked with a mode sense query.
Signed-off-by Fam Zheng <[email protected]>---
v2: Improve error message.
Fall back to a warning if mode sense failed.
Check unmarshal return value.--- block/iscsi.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)block/iscsi.c +22
-0@@ -1219,6 +1219,44 @@ static void iscsi_attach_aio_context(BlockDriverState *bs, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); } +static bool iscsi_is_write_protected (IscsiLun *iscsilun) +{ +struct scsi_task *task; +struct scsi_mode_sense *ms =NULL ; + + task =iscsi_modesense6_sync (iscsilun->iscsi, iscsilun->lun, +1 ,SCSI_MODESENSE_PC_CURRENT , +0x3F , +0 ,255 ); + +if (task ==NULL ) { +error_report ("Failed to send MODE_SENSE6 command: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + +if (task->status !=SCSI_STATUS_GOOD ) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + ms =scsi_datain_unmarshall (task);
block/iscsi.c static void iscsi_attach_aio_context(BlockDriverState *bs, · line 1253
Quoted 10 lines
block/iscsi.c +10
-0+if (!ms) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } +out: +if (task) { +scsi_free_scsi_task (task); + } +if (!ms) {
ms points to freed memory after scsi_free_scsi_task. furthermore the requests likely fails with task->status != SCSI_STATUS_GOOD if the modesense implementation is broken etc.
I would rework iscsi_is_write_protected to:
static bool iscsi_is_write_protected(IscsiLun *iscsilun)
{
struct scsi_task *task;
struct scsi_mode_sense *ms = NULL;
bool wrprotected = false;
task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1, SCSI_MODESENSE_PC_CURRENT,
0x3F,
0, 255);
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
goto fail;
}
ms = scsi_datain_unmarshall(task);
if (!ms) {
goto fail;
}
wrprotected = ms->device_specific_parameter & 0x80;
goto out;fail:
error_report("MODE_SENSE6 failed: %s. Assuming write enabled",
iscsi_get_error(iscsilun->iscsi));
out:
if (task) {
scsi_free_scsi_task(task);
}
return wrprotected;
}Peter
Quoted 24 lines
block/iscsi.c +14
-0+error_report ("Assuming write enabled" ); +return false; + } +return ms->device_specific_parameter &0x80 ; +} +/* * We support iscsi url's on the form * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> @@ -1339,6 +1377,14 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,scsi_free_scsi_task (task); task =NULL ; +/* Check the write protect flag of the LUN if we want to write */ +if ((flags &BDRV_O_RDWR ) + &&iscsi_is_write_protected (iscsilun)) { +error_setg (errp,"Cannot open a write protected LUN as read-write" ); + ret = -EPERM ; + goto out; + } +iscsi_readcapacity_sync (iscsilun, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Signature · 16 lines
Mit freundlichen Grüßen Peter Lieven ........................................................... KAMP Netzwerkdienste GmbH Vestische Str. 89-91 | 46117 Oberhausen Tel: +49 (0) 208.89 402-50 | Fax: +49 (0) 208.89 402-40 [email protected] | http://www.kamp.de Geschäftsführer: Heiner Lante | Michael Lante Amtsgericht Duisburg | HRB Nr. 12154 USt-Id-Nr.: DE 120607556 ...........................................................
Quoted 56 lines · Peter Lieven
Quoted 55 lines · Fam Zheng
Before, when a write protected iSCSI target is attached as scsi-disk with
BDRV_O_RDWR, we report it as writable, while in fact all writes will fail.One way to improve this is to report write protect flag as true to guest, but a even better way is to refuse using a write protected LUN to guest.
Target write protect flag is checked with a mode sense query.
Signed-off-by Fam Zheng <[email protected]>---
v2: Improve error message.
Fall back to a warning if mode sense failed.
Check unmarshal return value.--- block/iscsi.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)block/iscsi.c +27
-0@@ -1219,6 +1219,44 @@ static voidiscsi_attach_aio_context (BlockDriverState *bs,qemu_clock_get_ms (QEMU_CLOCK_REALTIME ) +NOP_INTERVAL ); } +static bool iscsi_is_write_protected(IscsiLun *iscsilun) +{ +struct scsi_task *task; +struct scsi_mode_sense *ms =NULL ; + + task =iscsi_modesense6_sync (iscsilun->iscsi, iscsilun->lun, +1 ,SCSI_MODESENSE_PC_CURRENT , +0x3F , +0 ,255 ); + +if (task ==NULL ) { +error_report ("Failed to send MODE_SENSE6 command: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + +if (task->status !=SCSI_STATUS_GOOD ) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + ms =scsi_datain_unmarshall (task); +if (!ms) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } +out:
Quoted 8 lines
block/iscsi.c static void iscsi_attach_aio_context(BlockDriverState *bs, · line 1253
Quoted 4 lines
block/iscsi.c +4
-0+if (task) { +scsi_free_scsi_task (task); + } +if (!ms) {ms points to freed memory after
scsi_free_scsi_task. furthermore the requests likely fails withtask->status!=SCSI_STATUS_GOODif the modesense implementation is broken etc.
This is a mix of your and Fam's code. Looks good?
static bool iscsi_is_write_protected(IscsiLun *iscsilun)
{
struct scsi_task *task;
struct scsi_mode_sense *ms = NULL;
bool wrprotected = false;
task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1, SCSI_MODESENSE_PC_CURRENT,
0x3F, 0, 255);
if (task == NULL) {
error_report("Failed to send MODE_SENSE(6) command: %s",
iscsi_get_error(iscsilun->iscsi));
goto out;
}
if (task->status != SCSI_STATUS_GOOD) {
error_report("MODE_SENSE(6) failed: %s",
iscsi_get_error(iscsilun->iscsi));
goto out;
}
ms = scsi_datain_unmarshall(task);
if (!ms) {
error_report("Failed to unmarshall MODE_SENSE(6) data: %s",
iscsi_get_error(iscsilun->iscsi));
goto out;
}
wrprotected = ms->device_specific_parameter & 0x80;out:
if (task) {
scsi_free_scsi_task(task);
}
return wrprotected;
}Paolo
Quoted 98 lines · Paolo Bonzini
Quoted 56 lines · Peter Lieven
Quoted 55 lines · Fam Zheng
Before, when a write protected iSCSI target is attached as scsi-disk with
BDRV_O_RDWR, we report it as writable, while in fact all writes will fail.One way to improve this is to report write protect flag as true to guest, but a even better way is to refuse using a write protected LUN to guest.
Target write protect flag is checked with a mode sense query.
Signed-off-by Fam Zheng <[email protected]>---
v2: Improve error message.
Fall back to a warning if mode sense failed.
Check unmarshal return value.--- block/iscsi.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)block/iscsi.c +27
-0@@ -1219,6 +1219,44 @@ static voidiscsi_attach_aio_context (BlockDriverState *bs,qemu_clock_get_ms (QEMU_CLOCK_REALTIME ) +NOP_INTERVAL ); } +static bool iscsi_is_write_protected(IscsiLun *iscsilun) +{ +struct scsi_task *task; +struct scsi_mode_sense *ms =NULL ; + + task =iscsi_modesense6_sync (iscsilun->iscsi, iscsilun->lun, +1 ,SCSI_MODESENSE_PC_CURRENT , +0x3F , +0 ,255 ); + +if (task ==NULL ) { +error_report ("Failed to send MODE_SENSE6 command: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + +if (task->status !=SCSI_STATUS_GOOD ) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + ms =scsi_datain_unmarshall (task); +if (!ms) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } +out:Quoted 8 lines
block/iscsi.c static void iscsi_attach_aio_context(BlockDriverState *bs, · line 1253
Quoted 4 lines
block/iscsi.c +4
-0+if (task) { +scsi_free_scsi_task (task); + } +if (!ms) {ms points to freed memory after
scsi_free_scsi_task. furthermore the requests likely fails withtask->status!=SCSI_STATUS_GOODif the modesense implementation is broken etc.This is a mix of your and Fam's code. Looks good?
static bool
iscsi_is_write_protected(IscsiLun *iscsilun){ struct scsi_task *task; struct scsi_mode_sense *ms = NULL; bool wrprotected = false; task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, 1, SCSI_MODESENSE_PC_CURRENT, 0x3F, 0, 255); if (task == NULL) { error_report("Failed to send MODE_SENSE(6) command: %s", iscsi_get_error(iscsilun->iscsi)); goto out; } if (task->status != SCSI_STATUS_GOOD) { error_report("MODE_SENSE(6) failed: %s", iscsi_get_error(iscsilun->iscsi)); goto out; } ms = scsi_datain_unmarshall(task); if (!ms) { error_report("Failed to unmarshall MODE_SENSE(6) data: %s", iscsi_get_error(iscsilun->iscsi)); goto out; } wrprotected = ms->device_specific_parameter & 0x80;
Quoted 6 lines
out:
if (task) { scsi_free_scsi_task(task); } return wrprotected; }
Looks good. Thanks!
Fam
Quoted 95 lines · Paolo Bonzini
Quoted 55 lines · Peter Lieven
Quoted 54 lines · Fam Zheng
Before, when a write protected iSCSI target is attached as scsi-disk with
BDRV_O_RDWR, we report it as writable, while in fact all writes will fail.One way to improve this is to report write protect flag as true to guest, but a even better way is to refuse using a write protected LUN to guest.
Target write protect flag is checked with a mode sense query.
Signed-off-by Fam Zheng <[email protected]>---
v2: Improve error message.
Fall back to a warning if mode sense failed. Check unmarshal return value.--- block/iscsi.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)block/iscsi.c +26
-0@@ -1219,6 +1219,44 @@ static voidiscsi_attach_aio_context (BlockDriverState *bs,qemu_clock_get_ms (QEMU_CLOCK_REALTIME ) +NOP_INTERVAL ); } +static bool iscsi_is_write_protected(IscsiLun *iscsilun) +{ +struct scsi_task *task; +struct scsi_mode_sense *ms =NULL ; + + task =iscsi_modesense6_sync (iscsilun->iscsi, iscsilun->lun, +1 ,SCSI_MODESENSE_PC_CURRENT , +0x3F , +0 ,255 ); + +if (task ==NULL ) { +error_report ("Failed to send MODE_SENSE6 command: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + +if (task->status !=SCSI_STATUS_GOOD ) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + } + ms =scsi_datain_unmarshall (task); +if (!ms) { +error_report ("MODE_SENSE6 failed: %s" , +iscsi_get_error (iscsilun->iscsi)); + goto out; + }Quoted 8 lines
block/iscsi.c static void iscsi_attach_aio_context(BlockDriverState *bs, · line 1253
Quoted 5 lines
block/iscsi.c +5
-0+out: +if (task) { +scsi_free_scsi_task (task); + } +if (!ms) {ms points to freed memory after
scsi_free_scsi_task. furthermore the requests likely fails withtask->status!=SCSI_STATUS_GOODif the modesense implementation is broken etc.This is a mix of your and Fam's code. Looks good?
static bool
iscsi_is_write_protected(IscsiLun *iscsilun){ struct scsi_task *task; struct scsi_mode_sense *ms = NULL; bool wrprotected = false; task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, 1, SCSI_MODESENSE_PC_CURRENT, 0x3F, 0, 255); if (task == NULL) { error_report("Failed to send MODE_SENSE(6) command: %s", iscsi_get_error(iscsilun->iscsi)); goto out; } if (task->status != SCSI_STATUS_GOOD) { error_report("MODE_SENSE(6) failed: %s", iscsi_get_error(iscsilun->iscsi)); goto out; } ms = scsi_datain_unmarshall(task); if (!ms) { error_report("Failed to unmarshall MODE_SENSE(6) data: %s", iscsi_get_error(iscsilun->iscsi)); goto out; } wrprotected = ms->device_specific_parameter & 0x80;
Quoted 6 lines
out:
if (task) { scsi_free_scsi_task(task); } return wrprotected; }
i would add the prefix "iSCSI: " to the error_reports as we have it for other outputs. (noticed this after writing my mail).
Otherwise looks good.
and actually also
Peter