Skip to contentSkip to conversation

scsi-disk: handle invalid cdb length

PATCH In review Bruce Rogers 2 participants 3 messages Apr 30, 2019 – May 1, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 scsi-disk: handle invalid cdb length

While investigating link-time-optimization, the compiler flagged this case of not handling the error return from scsi_cdb_length(). Handle this error case with a trace report.

---
 hw/scsi/scsi-disk.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/scsi/scsi-disk.c +4 -0
index e7e865ab3b..8fbf7512e5 100644
@@ -2520,6 +2520,10 @@ static void scsi_disk_new_request_dump(uint32_t lun, uint32_t tag, uint8_t *buf)
     int len = scsi_cdb_length(buf);
     char *line_buffer, *p;
 
+    if (len < 0) {
+        trace_scsi_disk_new_request(lun, tag, "bad cdb length");
+        return;
+    }
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {
Signature · 1 line
2.21.0
#1 lore raw
Quoted 14 lines · Bruce Rogers

While investigating link-time-optimization, the compiler flagged this case of not handling the error return from scsi_cdb_length(). Handle this error case with a trace report.

---
 hw/scsi/scsi-disk.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/scsi/scsi-disk.c +0 -0
index e7e865ab3b..8fbf7512e5 100644

hw/scsi/scsi-disk.c static void scsi_disk_new_request_dump(uint32_t lun, uint32_t tag, uint8_t *buf) · line 2524

Quoted 5 lines
hw/scsi/scsi-disk.c +2 -0
@@ -2520,6 +2520,10 @@ static void scsi_disk_new_request_dump(uint32_t lun, uint32_t tag, uint8_t *buf)
     int len = scsi_cdb_length(buf);
     char *line_buffer, *p;
 
+    if (len < 0) {
+        trace_scsi_disk_new_request(lun, tag, "bad cdb length");

This is going to print:

"Command: lun=%d tag=0x%x data=bad cdb length"

which is maybe not the best. I'd rather print something more direct, but it's probably better than actually rolling forward with len = -1.

Then again, this should literally never happen, because scsi_req_new is parsing the cdb object and already rejecting such cases.

Can you satisfy the compiler by asserting that it is greater than zero? It ought to be provably true.

--js

Quoted 6 lines
hw/scsi/scsi-disk.c +2 -0
+        return;
+    }
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {
#2 lore raw
Quoted 27 lines
Quoted 1 line
Quoted 1 line · John Snow

hw/scsi/scsi-disk.c static void scsi_disk_new_request_dump(uint32_t lun,

Quoted 14 lines · Bruce Rogers

While investigating link-time-optimization, the compiler flagged this case of not handling the error return from scsi_cdb_length(). Handle this error case with a trace report.

---
 hw/scsi/scsi-disk.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/scsi/scsi-disk.c +0 -0
index e7e865ab3b..8fbf7512e5 100644
@@ -2520,6 +2520,10 @@ static void scsi_disk_new_request_dump(uint32_t lun, 

uint32_t tag, uint8_t *buf)

hw/scsi/scsi-disk.c static void scsi_disk_new_request_dump(uint32_t lun, uint32_t tag, uint8_t *buf) · line 2524

Quoted 5 lines
hw/scsi/scsi-disk.c +2 -0
     int len = scsi_cdb_length(buf);
     char *line_buffer, *p;
 
+    if (len < 0) {
+        trace_scsi_disk_new_request(lun, tag, "bad cdb length");

This is going to print:

Quoted 8 lines

"Command: lun=%d tag=0x%x data=bad cdb length"

which is maybe not the best. I'd rather print something more direct, but it's probably better than actually rolling forward with len = -1.

Then again, this should literally never happen, because scsi_req_new is parsing the cdb object and already rejecting such cases.

Indeed, that is true.

Quoted 2 lines

Can you satisfy the compiler by asserting that it is greater than zero? It ought to be provably true.

Yes, that seems to work and is probably the way to go. I'll send a patch with that approach then. Thanks for the review.

Quoted 9 lines

--js

Quoted 6 lines
hw/scsi/scsi-disk.c +2 -0
+        return;
+    }
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {

Bruce