Skip to contentSkip to conversation

hw/scsi/megasas: Avoid buffer overrun in megasas_handle_scsi()

PATCH Superseded Philippe Mathieu-Daudé 3 participants 7 messages Dec 1, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/scsi/megasas: Avoid buffer overrun in megasas_handle_scsi()

FWIW megasas is not use by KVM.

Not sure what is the proper fix, but at least we
have a reproducer.

We might improve "scsi/utils" by adding length argument to scsi_cdb_length() and check valid there, but this will take time (large refactor). Add assertions there is too violent.

Philippe Mathieu-Daudé (3):
tests/qtest/fuzz-test: Quit test_lp1878642 once done hw/scsi/megasas: Assert cdb_len is valid in megasas_handle_scsi() hw/scsi/megasas: Have incorrect cdb return MFI_STAT_ABORT_NOT_POSSIBLE

 hw/scsi/megasas.c       |   6 ++
 tests/qtest/fuzz-test.c | 197 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 203 insertions(+)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/3 tests/qtest/fuzz-test: Quit test_lp1878642 once done

Missed in fd250172842 ("qtest: add a reproducer for LP#1878642").

---
 tests/qtest/fuzz-test.c | 1 +
 1 file changed, 1 insertion(+)
tests/qtest/fuzz-test.c +1 -0
index 9cb4c42bdea..87b72307a5b 100644
@@ -45,6 +45,7 @@ static void test_lp1878642_pci_bus_get_irq_level_assert(void)
     qtest_outl(s, 0xcf8, 0x8400f841);
     qtest_outl(s, 0xcfc, 0xebed205d);
     qtest_outl(s, 0x5d02, 0xebed205d);
+    qtest_quit(s);
 }
 
 int main(int argc, char **argv)
Signature · 1 line
2.26.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] tests/qtest/fuzz-test: Quit test_lp1878642 once done

Quoted 13 lines · Philippe Mathieu-Daudé

Missed in fd250172842 ("qtest: add a reproducer for LP#1878642").

---
 tests/qtest/fuzz-test.c | 1 +
 1 file changed, 1 insertion(+)
tests/qtest/fuzz-test.c +0 -0
index 9cb4c42bdea..87b72307a5b 100644

tests/qtest/fuzz-test.c static void test_lp1878642_pci_bus_get_irq_level_assert(void) · line 51

Quoted 7 lines
tests/qtest/fuzz-test.c +1 -0
@@ -45,6 +45,7 @@ static void test_lp1878642_pci_bus_get_irq_level_assert(void)
     qtest_outl(s, 0xcf8, 0x8400f841);
     qtest_outl(s, 0xcfc, 0xebed205d);
     qtest_outl(s, 0x5d02, 0xebed205d);
+    qtest_quit(s);
 }
 
 int main(int argc, char **argv)
#3 lore raw

PATCH 2/3 hw/scsi/megasas: Assert cdb_len is valid in megasas_handle_scsi()

In reply to Philippe Mathieu-Daudé

cdb_len can not be zero... (or less than 6) here, else we have a out-of-bound read first in scsi_cdb_length():

71 int scsi_cdb_length(uint8_t *buf)
72 {
73 int cdb_len;
74
75 switch (buf[0] >> 5) {
76 case 0:
77 cdb_len = 6;
78 break;

Then another out-of-bound read when the size returned by scsi_cdb_length() is used.

Add a reproducer which triggers:

  $ make check-qtest-x86_64
  Running test qtest-x86_64/fuzz-test
  qemu-system-x86_64: hw/scsi/megasas.c:1679: megasas_handle_scsi: Assertion `cdb_len > 0 && scsi_cdb_length(cdb) >= cdb_len' failed.
  tests/qtest/libqtest.c:181: kill_qemu() detected QEMU death from signal 6 (Aborted) (core dumped)
  ERROR qtest-x86_64/fuzz-test - too few tests run (expected 1, got 0)
---
 hw/scsi/megasas.c       |   1 +
 tests/qtest/fuzz-test.c | 196 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 197 insertions(+)
hw/scsi/megasas.c +1 -0
index 1a5fc5857db..28efd094111 100644
@@ -1676,6 +1676,7 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
     lun_id = cmd->frame->header.lun_id;
     cdb_len = cmd->frame->header.cdb_len;
 
+    assert(cdb_len > 0 && scsi_cdb_length(cdb) >= cdb_len);
     if (is_logical) {
         if (target_id >= MFI_MAX_LD || lun_id != 0) {
             trace_megasas_scsi_target_not_present(
tests/qtest/fuzz-test.c +196 -0

208 lines not on this page · raw message

Signature · 1 line
2.26.2
#4 lore raw

Re: [PATCH 2/3] hw/scsi/megasas: Assert cdb_len is valid in megasas_handle_scsi()

Quoted 23 lines · Philippe Mathieu-Daudé

cdb_len can not be zero... (or less than 6) here, else we have a out-of-bound read first in scsi_cdb_length():

71 int scsi_cdb_length(uint8_t *buf)
72 {
73 int cdb_len;
74
75 switch (buf[0] >> 5) {
76 case 0:
77 cdb_len = 6;
78 break;

Then another out-of-bound read when the size returned by scsi_cdb_length() is used.

Add a reproducer which triggers:

  $ make check-qtest-x86_64
  Running test qtest-x86_64/fuzz-test
  qemu-system-x86_64: hw/scsi/megasas.c:1679: megasas_handle_scsi: Assertion `cdb_len > 0 && scsi_cdb_length(cdb) >= cdb_len' failed.
  tests/qtest/libqtest.c:181: kill_qemu() detected QEMU death from signal 6 (Aborted) (core dumped)
  ERROR qtest-x86_64/fuzz-test - too few tests run (expected 1, got 0)
Quoted 5 lines
---
 hw/scsi/megasas.c       |   1 +
 tests/qtest/fuzz-test.c | 196 ++++++++++++++++++++++++++++++++++++++++

For the final version, I think you should add the fuzz-test after the fix (patch 3) ... otherwise this breaks bisection later...

Thomas

#5 lore raw

PATCH 1/1 hw/scsi/megasas: Assert cdb_len is valid in megasas_handle_scsi()

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

cdb_len can not be zero... (or less than 6) here, else we have a out-of-bound read first in scsi_cdb_length():

71 int scsi_cdb_length(uint8_t *buf)
72 {
73 int cdb_len;
74
75 switch (buf[0] >> 5) {
76 case 0:
77 cdb_len = 6;
78 break;

Then another out-of-bound read when the size returned by scsi_cdb_length() is used.

Add a reproducer which triggers:

  $ make check-qtest-x86_64
  Running test qtest-x86_64/fuzz-test
  qemu-system-x86_64: hw/scsi/megasas.c:1679: megasas_handle_scsi: Assertion `cdb_len > 0 && scsi_cdb_length(cdb) >= cdb_len' failed.
  tests/qtest/libqtest.c:181: kill_qemu() detected QEMU death from signal 6 (Aborted) (core dumped)
  ERROR qtest-x86_64/fuzz-test - too few tests run (expected 1, got 0)

---

Ran it through the minimizer - No more blobs.

 hw/scsi/megasas.c       |  1 +
 tests/qtest/fuzz-test.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
hw/scsi/megasas.c +1 -0
index 1a5fc5857d..28efd09411 100644
@@ -1676,6 +1676,7 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
     lun_id = cmd->frame->header.lun_id;
     cdb_len = cmd->frame->header.cdb_len;
 
+    assert(cdb_len > 0 && scsi_cdb_length(cdb) >= cdb_len);
     if (is_logical) {
         if (target_id >= MFI_MAX_LD || lun_id != 0) {
             trace_megasas_scsi_target_not_present(
tests/qtest/fuzz-test.c +19 -0
index 87b72307a5..31f90cfb4f 100644
@@ -48,6 +48,23 @@ static void test_lp1878642_pci_bus_get_irq_level_assert(void)
     qtest_quit(s);
 }
 
+static void test_megasas_cdb_len_zero(void)
+{
+    QTestState *s;
+
+    s = qtest_init("-M pc -nodefaults "
+                   "-device megasas-gen2 -device scsi-cd,drive=null0 "
+                   "-blockdev driver=null-co,read-zeroes=on,node-name=null0");
+
+    qtest_outl(s, 0xcf8, 0x80001011);
+    qtest_outb(s, 0xcfc, 0xbb);
+    qtest_outl(s, 0xcf8, 0x80001002);
+    qtest_outl(s, 0xcfc, 0xf3ff2966);
+    qtest_writeb(s, 0x4600, 0x03);
+    qtest_outw(s, 0xbb40, 0x460b);
+    qtest_quit(s);
+}
+
 int main(int argc, char **argv)
 {
     const char *arch = qtest_get_arch();
@@ -59,6 +76,8 @@ int main(int argc, char **argv)
                        test_lp1878263_megasas_zero_iov_cnt);
         qtest_add_func("fuzz/test_lp1878642_pci_bus_get_irq_level_assert",
                        test_lp1878642_pci_bus_get_irq_level_assert);
+        qtest_add_func("fuzz/test_megasas_cdb_len_zero",
+                       test_megasas_cdb_len_zero);
     }
 
     return g_test_run();
Signature · 1 line
2.28.0
#6 lore raw

RFC PATCH 3/3 hw/scsi/megasas: Have incorrect cdb return MFI_STAT_ABORT_NOT_POSSIBLE

In reply to Philippe Mathieu-Daudé

Avoid out-of-bound array access with invalid CDB is provided.

---
RFC because I have no clue how hardware works. Maybe returning MFI_STAT_ARRAY_INDEX_INVALID is better? Do we need to call megasas_write_sense()?

 hw/scsi/megasas.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/scsi/megasas.c +6 -1
index 28efd094111..d89a3c8c3ce 100644
@@ -1676,7 +1676,12 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
     lun_id = cmd->frame->header.lun_id;
     cdb_len = cmd->frame->header.cdb_len;
 
-    assert(cdb_len > 0 && scsi_cdb_length(cdb) >= cdb_len);
+    if (!cdb_len || scsi_cdb_length(cdb) < cdb_len) {
+        trace_megasas_scsi_invalid_cdb_len(mfi_frame_desc(frame_cmd),
+                                           is_logical, target_id,
+                                           lun_id, cdb_len);
+        return MFI_STAT_ABORT_NOT_POSSIBLE;
+    }
     if (is_logical) {
         if (target_id >= MFI_MAX_LD || lun_id != 0) {
             trace_megasas_scsi_target_not_present(
Signature · 1 line
2.26.2