Skip to contentSkip to conversation

iscsi: Refuse to open as writable if the LUN is write protected

PATCH Superseded Fam Zheng 3 participants 8 messages Oct 29, 2014 – Oct 30, 2014 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 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.

---
 block/iscsi.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
block/iscsi.c +30 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
+        if (ms->device_specific_parameter & 0x80) {
+            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
#1 lore raw
Quoted 24 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.

---
 block/iscsi.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1352

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Paolo

Quoted 23 lines
block/iscsi.c +19 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
+        if (ms->device_specific_parameter & 0x80) {
+            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);
#2 lore raw

Am 29.10.2014 um 14:31 schrieb Paolo Bonzini:

Quoted 38 lines · Fam Zheng
Quoted 24 lines

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.

---
 block/iscsi.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1352

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Quoted 14 lines

Paolo

Quoted 12 lines
block/iscsi.c +12 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);

scsi_datain_unmarshall may fail. You need to check for NULL here.

Peter

Quoted 11 lines
Quoted 11 lines
block/iscsi.c +7 -0
+        if (ms->device_specific_parameter & 0x80) {
+            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);
#3 lore raw
Quoted 54 lines · Peter Lieven

Am 29.10.2014 um 14:31 schrieb Paolo Bonzini:

Quoted 38 lines · Fam Zheng
Quoted 24 lines

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.

---
 block/iscsi.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1352

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Quoted 14 lines

Paolo

Quoted 12 lines
block/iscsi.c +12 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
Quoted 1 line

scsi_datain_unmarshall may fail. You need to check for NULL here.

Thanks for the remark, I fixed this too.

Paolo

#4 lore raw

Am 29.10.2014 um 19:28 schrieb Paolo Bonzini <[email protected]>:

Quoted 55 lines · Peter Lieven
Quoted 54 lines

Am 29.10.2014 um 14:31 schrieb Paolo Bonzini:

Quoted 52 lines · Fam Zheng
Quoted 24 lines

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.

---
block/iscsi.c | 30 ++++++++++++++++++++++++++++++

1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1352

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Paolo

Quoted 12 lines
block/iscsi.c +12 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
Quoted 3 lines
Quoted 1 line

scsi_datain_unmarshall may fail. You need to check for NULL here.

Thanks for the remark, I fixed this too.

Is it in your repo? I can do some testing tomorrow ;-)

Peter

#5 lore raw
In reply to Paolo Bonzini
Quoted 53 lines · Paolo Bonzini
Quoted 52 lines · Peter Lieven

Am 29.10.2014 um 14:31 schrieb Paolo Bonzini:

Quoted 51 lines · Fam Zheng
Quoted 24 lines

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.

---
  block/iscsi.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1352

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Paolo

Quoted 12 lines
block/iscsi.c +12 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
Quoted 4 lines
Quoted 1 line

scsi_datain_unmarshall may fail. You need to check for NULL here.

Thanks for the remark, I fixed this too.

Paolo

I am not 100% happy with this patch as it may break support for some targets.

It seems that MODESENSE is somewhat tricky with some targets. See function sd_read_write_protect_flag in <http://lxr.free-electrons.com/ident?i=sd_read_write_protect_flag>drivers/scsi/sd.c of the Linux kernel.

I would not fail if this modesense fails, but just assume write enabled. Maybe drop a warning.

In the command itself:
0x3F => SCSI_MODEPAGE_RETURN_ALL_PAGES

Can you please post the resulting patch somewhere for review.

Thanks,
Peter

author
#6 lore raw
Quoted 56 lines · Peter Lieven
Quoted 55 lines · Paolo Bonzini
Quoted 51 lines · Peter Lieven

Am 29.10.2014 um 14:31 schrieb Paolo Bonzini:

Quoted 36 lines · Fam Zheng
Quoted 23 lines

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.

---
 block/iscsi.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1351

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Quoted 14 lines

Paolo

Quoted 12 lines
block/iscsi.c +12 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
Quoted 1 line

scsi_datain_unmarshall may fail. You need to check for NULL here.

Thanks for the remark, I fixed this too.

Quoted 8 lines
Quoted 1 line

Paolo

I am not 100% happy with this patch as it may break support for some targets.

It seems that MODESENSE is somewhat tricky with some targets. See function sd_read_write_protect_flag in <http://lxr.free-electrons.com/ident?i=sd_read_write_protect_flag>drivers/scsi/sd.c of the Linux kernel.

I would not fail if this modesense fails, but just assume write enabled. Maybe drop a warning.

OK.

Quoted 3 lines

In the command itself:
0x3F => SCSI_MODEPAGE_RETURN_ALL_PAGES

That requires a newer version of libiscsi than we currently check for. Seeing drivers/scsi/sd.c used 0x3F, I left this.

Quoted 2 lines

Can you please post the resulting patch somewhere for review.

I'll fix this with the error messsage and unmarshal failure and post another version.

Fam

#7 lore raw
Quoted 63 lines · Fam Zheng
Quoted 62 lines · Peter Lieven
Quoted 52 lines · Paolo Bonzini
Quoted 51 lines · Peter Lieven

Am 29.10.2014 um 14:31 schrieb Paolo Bonzini:

Quoted 50 lines · Fam Zheng
Quoted 23 lines

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.

---
  block/iscsi.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
block/iscsi.c +1 -0
index 233f462..c154928 100644
@@ -1339,6 +1339,36 @@ 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 */

block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1351

Quoted 10 lines
block/iscsi.c +10 -0
+    if (flags & BDRV_O_RDWR) {
+        struct scsi_mode_sense *ms;
+
+        task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
+                                      1, SCSI_MODESENSE_PC_CURRENT,
+                                      0x3F,
+                                      0, 255);
+
+        if (task == NULL) {
+            error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n",

This is MODE SENSE(6). Fixed and applied.

Paolo

Quoted 12 lines
block/iscsi.c +12 -0
+                       iscsi_get_error(iscsilun->iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+
+        if (task->status != SCSI_STATUS_GOOD) {
+            error_setg(errp, "MODE_SENSE10 failed: %s\n",
+                       iscsi_get_error(iscsi));
+            ret = -EINVAL;
+            goto out;
+        }
+        ms = scsi_datain_unmarshall(task);
Quoted 4 lines
Quoted 1 line

scsi_datain_unmarshall may fail. You need to check for NULL here.

Thanks for the remark, I fixed this too.

Paolo

I am not 100% happy with this patch as it may break support for some targets.

It seems that MODESENSE is somewhat tricky with some targets. See function sd_read_write_protect_flag in <http://lxr.free-electrons.com/ident?i=sd_read_write_protect_flag>drivers/scsi/sd.c of the Linux kernel.

Quoted 7 lines
Quoted 1 line

I would not fail if this modesense fails, but just assume write enabled. Maybe drop a warning.

OK.

Quoted 2 lines

In the command itself:
0x3F => SCSI_MODEPAGE_RETURN_ALL_PAGES

That requires a newer version of libiscsi than we currently check for. Seeing drivers/scsi/sd.c used 0x3F, I left this.

Okay, I wasn't ware. It seems it went in for 1.10.0 and we check for 1.9.0, right?

Quoted 4 lines
Quoted 2 lines

Can you please post the resulting patch somewhere for review.

I'll fix this with the error messsage and unmarshal failure and post another version.

Thanks,
Peter