Skip to contentSkip to conversation

scsi: Silence gcc warning

PATCH Applied Eric Blake 3 participants 4 messages Feb 9, 2021 – Mar 9, 2021

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 scsi: Silence gcc warning

On Fedora 33, gcc 10.2.1 notes that scsi_cdb_length(buf) can set len==-1, which in turn overflows g_malloc():

[5/5] Linking target qemu-system-x86_64
In function ‘scsi_disk_new_request_dump’,
inlined from ‘scsi_new_request’ at ../hw/scsi/scsi-disk.c:2608:9:
../hw/scsi/scsi-disk.c:2582:19: warning: argument 1 value ‘18446744073709551612’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
2582 | line_buffer = g_malloc(len * 5 + 1);
| ^

Silence it with a decent assertion, since we only convert a buffer to bytes when we have a valid cdb length.

---
 hw/scsi/scsi-disk.c | 1 +
 1 file changed, 1 insertion(+)
hw/scsi/scsi-disk.c +1 -0
index ed52fcd49ff0..b3311a5657b7 100644
@@ -2579,6 +2579,7 @@ 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;
 
+    assert(len > 0 && len <= 16);
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {
Signature · 1 line
2.30.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Eric Blake

On Fedora 33, gcc 10.2.1 notes that scsi_cdb_length(buf) can set len==-1, which in turn overflows g_malloc():

[5/5] Linking target qemu-system-x86_64
In function ‘scsi_disk_new_request_dump’,
inlined from ‘scsi_new_request’ at ../hw/scsi/scsi-disk.c:2608:9:
../hw/scsi/scsi-disk.c:2582:19: warning: argument 1 value ‘18446744073709551612’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
2582 | line_buffer = g_malloc(len * 5 + 1);
| ^

Silence it with a decent assertion, since we only convert a buffer to bytes when we have a valid cdb length.

---
 hw/scsi/scsi-disk.c | 1 +
 1 file changed, 1 insertion(+)
hw/scsi/scsi-disk.c +0 -0
index ed52fcd49ff0..b3311a5657b7 100644

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

Quoted 7 lines
hw/scsi/scsi-disk.c +1 -0
@@ -2579,6 +2579,7 @@ 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;
 
+    assert(len > 0 && len <= 16);
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {
author
#2 lore raw

Adding qemu-trivial in cc.

Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Eric Blake

On Fedora 33, gcc 10.2.1 notes that scsi_cdb_length(buf) can set len==-1, which in turn overflows g_malloc():

[5/5] Linking target qemu-system-x86_64
In function ‘scsi_disk_new_request_dump’,
inlined from ‘scsi_new_request’ at ../hw/scsi/scsi-disk.c:2608:9:
../hw/scsi/scsi-disk.c:2582:19: warning: argument 1 value ‘18446744073709551612’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
2582 | line_buffer = g_malloc(len * 5 + 1);
| ^

Silence it with a decent assertion, since we only convert a buffer to bytes when we have a valid cdb length.

---
 hw/scsi/scsi-disk.c | 1 +
 1 file changed, 1 insertion(+)
hw/scsi/scsi-disk.c +0 -0
index ed52fcd49ff0..b3311a5657b7 100644

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

Quoted 7 lines
hw/scsi/scsi-disk.c +1 -0
@@ -2579,6 +2579,7 @@ 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;
 
+    assert(len > 0 && len <= 16);
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#3 lore raw

Le 08/03/2021 à 20:54, Eric Blake a écrit :

Quoted 39 lines

Adding qemu-trivial in cc.

Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Eric Blake

On Fedora 33, gcc 10.2.1 notes that scsi_cdb_length(buf) can set len==-1, which in turn overflows g_malloc():

[5/5] Linking target qemu-system-x86_64
In function ‘scsi_disk_new_request_dump’,
inlined from ‘scsi_new_request’ at ../hw/scsi/scsi-disk.c:2608:9:
../hw/scsi/scsi-disk.c:2582:19: warning: argument 1 value ‘18446744073709551612’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
2582 | line_buffer = g_malloc(len * 5 + 1);
| ^

Silence it with a decent assertion, since we only convert a buffer to bytes when we have a valid cdb length.

---
 hw/scsi/scsi-disk.c | 1 +
 1 file changed, 1 insertion(+)
hw/scsi/scsi-disk.c +0 -0
index ed52fcd49ff0..b3311a5657b7 100644

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

Quoted 7 lines
hw/scsi/scsi-disk.c +1 -0
@@ -2579,6 +2579,7 @@ 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;
 
+    assert(len > 0 && len <= 16);
     line_buffer = g_malloc(len * 5 + 1);
 
     for (i = 0, p = line_buffer; i < len; i++) {

Applied to my trivial-patches branch.

Thanks,
Laurent