Skip to contentSkip to conversation

scsi: megasas: Internal cdbs have 16-byte length

PATCH Applied Guenter Roeck 6 participants 10 messages Feb 28, 2023 – Nov 21, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: megasas: Internal cdbs have 16-byte length

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

---
 hw/scsi/megasas.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
hw/scsi/megasas.c +2 -12
index 9cbbb16121..d624866bb6 100644
@@ -1780,7 +1780,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
     uint8_t cdb[16];
     int len;
     struct SCSIDevice *sdev = NULL;
-    int target_id, lun_id, cdb_len;
+    int target_id, lun_id;
 
     lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
     lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
@@ -1789,7 +1789,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
     target_id = cmd->frame->header.target_id;
     lun_id = cmd->frame->header.lun_id;
-    cdb_len = cmd->frame->header.cdb_len;
 
     if (target_id < MFI_MAX_LD && lun_id == 0) {
         sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
@@ -1804,15 +1803,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
         return MFI_STAT_DEVICE_NOT_FOUND;
     }
 
-    if (cdb_len > 16) {
-        trace_megasas_scsi_invalid_cdb_len(
-            mfi_frame_desc(frame_cmd), 1, target_id, lun_id, cdb_len);
-        megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
-        cmd->frame->header.scsi_status = CHECK_CONDITION;
-        s->event_count++;
-        return MFI_STAT_SCSI_DONE_WITH_ERROR;
-    }
-
     cmd->iov_size = lba_count * sdev->blocksize;
     if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) {
         megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
@@ -1823,7 +1813,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
     megasas_encode_lba(cdb, lba_start, lba_count, is_write);
     cmd->req = scsi_req_new(sdev, cmd->index,
-                            lun_id, cdb, cdb_len, cmd);
+                            lun_id, cdb, sizeof(cdb), cmd);
     if (!cmd->req) {
         trace_megasas_scsi_req_alloc_failed(
             mfi_frame_desc(frame_cmd), target_id, lun_id);
Signature · 1 line
2.39.1
Reviewed-by on v1 1/1
#1 lore raw

+Hannes

Quoted 50 lines · Guenter Roeck

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

---
  hw/scsi/megasas.c | 14 ++------------
  1 file changed, 2 insertions(+), 12 deletions(-)
hw/scsi/megasas.c +1 -11
index 9cbbb16121..d624866bb6 100644
@@ -1780,7 +1780,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
      uint8_t cdb[16];
      int len;
      struct SCSIDevice *sdev = NULL;
-    int target_id, lun_id, cdb_len;
+    int target_id, lun_id;
 
      lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
      lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
@@ -1789,7 +1789,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
      target_id = cmd->frame->header.target_id;
      lun_id = cmd->frame->header.lun_id;
-    cdb_len = cmd->frame->header.cdb_len;
 
      if (target_id < MFI_MAX_LD && lun_id == 0) {
          sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
@@ -1804,15 +1803,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
          return MFI_STAT_DEVICE_NOT_FOUND;
      }
 
-    if (cdb_len > 16) {
-        trace_megasas_scsi_invalid_cdb_len(
-            mfi_frame_desc(frame_cmd), 1, target_id, lun_id, cdb_len);
-        megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
-        cmd->frame->header.scsi_status = CHECK_CONDITION;
-        s->event_count++;
-        return MFI_STAT_SCSI_DONE_WITH_ERROR;
-    }
-
      cmd->iov_size = lba_count * sdev->blocksize;
      if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) {
          megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));

hw/scsi/megasas.c static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd) · line 1816

Quoted 5 lines
hw/scsi/megasas.c +1 -1
@@ -1823,7 +1813,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
      megasas_encode_lba(cdb, lba_start, lba_count, is_write);
      cmd->req = scsi_req_new(sdev, cmd->index,
-                            lun_id, cdb, cdb_len, cmd);
+                            lun_id, cdb, sizeof(cdb), cmd);

I haven't checked the spec, but this logic makes sense to me, so:

Quoted 3 lines
hw/scsi/megasas.c +0 -0
      if (!cmd->req) {
          trace_megasas_scsi_req_alloc_failed(
              mfi_frame_desc(frame_cmd), target_id, lun_id);
#2 lore raw
In reply to Guenter Roeck
Quoted 6 lines · Guenter Roeck

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

This smells like a -stable material (CC qemu-stable@).

/mjt

#3 lore raw
Quoted 7 lines
Quoted 6 lines · Guenter Roeck

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

Has this been lost/forgotten?

/mjt

#4 lore raw
Quoted 10 lines · Michael Tokarev
Quoted 7 lines
Quoted 6 lines · Guenter Roeck

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

Has this been lost/forgotten?

Not sure. My understanding was that I could not prove that this is how real hardware handles the situation, thus it wasn't applied. I carry it locally in my builds of qemu, so it is not a problem for me. Note that I didn't check if the problem has since been fixed differently. Maybe that is the case and the problem no longer exists in the upstream version of qemu.

Guenter

Reviewed-by
#5 lore raw
Quoted 12 lines · Guenter Roeck
Quoted 10 lines · Michael Tokarev
Quoted 7 lines
Quoted 6 lines · Guenter Roeck

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

Has this been lost/forgotten?

Quoted 6 lines

Not sure. My understanding was that I could not prove that this is how real hardware handles the situation, thus it wasn't applied. I carry it locally in my builds of qemu, so it is not a problem for me. Note that I didn't check if the problem has since been fixed differently. Maybe that is the case and the problem no longer exists in the upstream version of qemu.

The issue still happens upstream and I have just written the exact same fix for it, so I'm adding my:

Tested-by on v1 1/1
#6 lore raw
In reply to Guenter Roeck

Am 28.02.23 um 18:11 schrieb Guenter Roeck:

Quoted 7 lines

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

But I do have a question:

Quoted 28 lines
---
 hw/scsi/megasas.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
hw/scsi/megasas.c +1 -2
index 9cbbb16121..d624866bb6 100644
@@ -1780,7 +1780,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
     uint8_t cdb[16];
     int len;
     struct SCSIDevice *sdev = NULL;
-    int target_id, lun_id, cdb_len;
+    int target_id, lun_id;
 
     lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
     lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
@@ -1789,7 +1789,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
     target_id = cmd->frame->header.target_id;
     lun_id = cmd->frame->header.lun_id;
-    cdb_len = cmd->frame->header.cdb_len;
 
     if (target_id < MFI_MAX_LD && lun_id == 0) {
         sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
@@ -1804,15 +1803,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
         return MFI_STAT_DEVICE_NOT_FOUND;

hw/scsi/megasas.c static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd) · line 1814

Quoted 10 lines
hw/scsi/megasas.c +0 -8
     }
 
-    if (cdb_len > 16) {
-        trace_megasas_scsi_invalid_cdb_len(
-            mfi_frame_desc(frame_cmd), 1, target_id, lun_id, cdb_len);
-        megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
-        cmd->frame->header.scsi_status = CHECK_CONDITION;
-        s->event_count++;
-        return MFI_STAT_SCSI_DONE_WITH_ERROR;
-    }

Shouldn't we still fail when cmd->frame->header.cdb_len > 16? Or is the consequence of

Quoted 1 line

Host drivers do not necessarily set cdb_len in megasas io commands.

that this can be uninitialized memory and we need to assume it was not explicitly set?

Best Regards,
Fiona

Quoted 13 lines
hw/scsi/megasas.c +1 -2
-
     cmd->iov_size = lba_count * sdev->blocksize;
     if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) {
         megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
@@ -1823,7 +1813,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
     megasas_encode_lba(cdb, lba_start, lba_count, is_write);
     cmd->req = scsi_req_new(sdev, cmd->index,
-                            lun_id, cdb, cdb_len, cmd);
+                            lun_id, cdb, sizeof(cdb), cmd);
     if (!cmd->req) {
         trace_megasas_scsi_req_alloc_failed(
             mfi_frame_desc(frame_cmd), target_id, lun_id);
#7 lore raw
Quoted 55 lines · Fiona Ebner

Am 28.02.23 um 18:11 schrieb Guenter Roeck:

Quoted 7 lines

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

But I do have a question:

Quoted 28 lines
---
  hw/scsi/megasas.c | 14 ++------------
  1 file changed, 2 insertions(+), 12 deletions(-)
hw/scsi/megasas.c +1 -2
index 9cbbb16121..d624866bb6 100644
@@ -1780,7 +1780,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
      uint8_t cdb[16];
      int len;
      struct SCSIDevice *sdev = NULL;
-    int target_id, lun_id, cdb_len;
+    int target_id, lun_id;
 
      lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
      lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
@@ -1789,7 +1789,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
      target_id = cmd->frame->header.target_id;
      lun_id = cmd->frame->header.lun_id;
-    cdb_len = cmd->frame->header.cdb_len;
 
      if (target_id < MFI_MAX_LD && lun_id == 0) {
          sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
@@ -1804,15 +1803,6 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
          return MFI_STAT_DEVICE_NOT_FOUND;

hw/scsi/megasas.c static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd) · line 1814

Quoted 10 lines
hw/scsi/megasas.c +0 -8
      }
 
-    if (cdb_len > 16) {
-        trace_megasas_scsi_invalid_cdb_len(
-            mfi_frame_desc(frame_cmd), 1, target_id, lun_id, cdb_len);
-        megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
-        cmd->frame->header.scsi_status = CHECK_CONDITION;
-        s->event_count++;
-        return MFI_STAT_SCSI_DONE_WITH_ERROR;
-    }

Shouldn't we still fail when cmd->frame->header.cdb_len > 16? Or is the consequence of

Quoted 5 lines
Quoted 1 line

Host drivers do not necessarily set cdb_len in megasas io commands.

that this can be uninitialized memory and we need to assume it was not explicitly set?

I doubt that real hardware uses or checks the field for the affected commands because that would be pointless, but it is really up to you to decide how you want to handle it.

Guenter

Quoted 17 lines

Best Regards,
Fiona

Quoted 13 lines
hw/scsi/megasas.c +1 -2
-
      cmd->iov_size = lba_count * sdev->blocksize;
      if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) {
          megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));
@@ -1823,7 +1813,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
 
      megasas_encode_lba(cdb, lba_start, lba_count, is_write);
      cmd->req = scsi_req_new(sdev, cmd->index,
-                            lun_id, cdb, cdb_len, cmd);
+                            lun_id, cdb, sizeof(cdb), cmd);
      if (!cmd->req) {
          trace_megasas_scsi_req_alloc_failed(
              mfi_frame_desc(frame_cmd), target_id, lun_id);
#8 lore raw

Am 03.03.23 um 16:10 schrieb Guenter Roeck:

Quoted 61 lines · Fiona Ebner
Quoted 54 lines

Am 28.02.23 um 18:11 schrieb Guenter Roeck:

Quoted 7 lines

Host drivers do not necessarily set cdb_len in megasas io commands. With commits 6d1511cea0 ("scsi: Reject commands if the CDB length exceeds buf_len") and fe9d8927e2 ("scsi: Add buf_len parameter to scsi_req_new()"), this results in failures to boot Linux from affected SCSI drives because cdb_len is set to 0 by the host driver. Set the cdb length to its actual size to solve the problem.

But I do have a question:

Quoted 27 lines

---
  hw/scsi/megasas.c | 14 ++------------

  1 file changed, 2 insertions(+), 12 deletions(-)
hw/scsi/megasas.c +1 -2
index 9cbbb16121..d624866bb6 100644
@@ -1780,7 +1780,7 @@ static int megasas_handle_io(MegasasState *s,
 MegasasCmd *cmd, int frame_cmd)
       uint8_t cdb[16];
       int len;
       struct SCSIDevice *sdev = NULL;
-    int target_id, lun_id, cdb_len;
+    int target_id, lun_id;
         lba_count = le32_to_cpu(cmd->frame->io.header.data_len);
       lba_start_lo = le32_to_cpu(cmd->frame->io.lba_lo);
@@ -1789,7 +1789,6 @@ static int megasas_handle_io(MegasasState *s,
 MegasasCmd *cmd, int frame_cmd)
         target_id = cmd->frame->header.target_id;
       lun_id = cmd->frame->header.lun_id;
-    cdb_len = cmd->frame->header.cdb_len;
         if (target_id < MFI_MAX_LD && lun_id == 0) {
           sdev = scsi_device_find(&s->bus, 0, target_id, lun_id);
@@ -1804,15 +1803,6 @@ static int megasas_handle_io(MegasasState *s,
 MegasasCmd *cmd, int frame_cmd)

hw/scsi/megasas.c static int megasas_handle_io(MegasasState *s, · line 1814

Quoted 10 lines
hw/scsi/megasas.c +0 -7
           return MFI_STAT_DEVICE_NOT_FOUND;
       }
   -    if (cdb_len > 16) {
-        trace_megasas_scsi_invalid_cdb_len(
-            mfi_frame_desc(frame_cmd), 1, target_id, lun_id, cdb_len);
-        megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
-        cmd->frame->header.scsi_status = CHECK_CONDITION;
-        s->event_count++;
-        return MFI_STAT_SCSI_DONE_WITH_ERROR;
-    }

Shouldn't we still fail when cmd->frame->header.cdb_len > 16? Or is the consequence of

Quoted 5 lines
Quoted 1 line

Host drivers do not necessarily set cdb_len in megasas io commands.

that this can be uninitialized memory and we need to assume it was not explicitly set?

Quoted 6 lines

I doubt that real hardware uses or checks the field for the affected commands
because that would be pointless, but it is really up to you to decide how you want to handle it.

Guenter

Okay, thank you for the explanation!

Quoted 21 lines
Quoted 18 lines

Best Regards,
Fiona

Quoted 13 lines

-
      cmd->iov_size = lba_count * sdev->blocksize;
      if (megasas_map_sgl(s, cmd, &cmd->frame->io.sgl)) { megasas_write_sense(cmd, SENSE_CODE(TARGET_FAILURE));

hw/scsi/megasas.c +1 -1
@@ -1823,7 +1813,7 @@ static int megasas_handle_io(MegasasState *s,
 MegasasCmd *cmd, int frame_cmd)
         megasas_encode_lba(cdb, lba_start, lba_count, is_write);
       cmd->req = scsi_req_new(sdev, cmd->index,
-                            lun_id, cdb, cdb_len, cmd);
+                            lun_id, cdb, sizeof(cdb), cmd);
       if (!cmd->req) {

          trace_megasas_scsi_req_alloc_failed( mfi_frame_desc(frame_cmd), target_id, lun_id);