Skip to contentSkip to conversation

fix some warnings by static code scan tool

PATCH RESEND In review Longpeng(Mike) 5 participants 23 messages Feb 24, 2020 – Mar 12, 2020 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

cover fix some warnings by static code scan tool

From: Longpeng <[email protected]>

Hi guys,
Our tool find some potential issues in QEMU source code, maybe they're misreported, hope you could have a look at them.

Longpeng (Mike) (3):
vfio/pci: fix a null pointer reference in vfio_rom_read
vhost: fix a null pointer reference of vhost_log util/pty: fix a null pointer reference in qemu_openpty_raw

 hw/vfio/pci.c       | 13 ++++++++-----
 hw/virtio/vhost.c   | 19 +++++++++++++++++--
 util/qemu-openpty.c | 10 ++--------
 3 files changed, 27 insertions(+), 15 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH RESEND 1/3 vfio/pci: fix a null pointer reference in vfio_rom_read

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +8 -5
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
     uint64_t data = 0;
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 53 lines · On Mon, 24 Feb 2020 14:42:17 +0800 "Longpeng(Mike)" <[email protected]>…

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +5 -3
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
     uint64_t data = 0;

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 873

Quoted 10 lines
hw/vfio/pci.c +3 -2
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,

Looks like an obvious bug, until you look at the rest of this memcpy():

memcpy(&val, vdev->rom + addr,

           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

IOW, we'll do a zero sized memcpy() if rom_size is zero, so there's no risk of the concern identified in the commit log. This patch is unnecessary. Thanks,

Alex

#3 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 69 lines · Alex Williamson
Quoted 53 lines · On Mon, 24 Feb 2020 14:42:17 +0800 "Longpeng(Mike)" <[email protected]>…

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +5 -3
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
     uint64_t data = 0;

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 873

Quoted 10 lines
hw/vfio/pci.c +3 -2
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,

Looks like an obvious bug, until you look at the rest of this memcpy():

Quoted 7 lines

memcpy(&val, vdev->rom + addr,

           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

IOW, we'll do a zero sized memcpy() if rom_size is zero, so there's no risk of the concern identified in the commit log. This patch is unnecessary. Thanks,

Oh, I missed that, sorry for make the noise, thanks

Quoted 4 lines

Alex

.

#4 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 70 lines · Cloud Infrastructure Service Product Dept.)
Quoted 69 lines · Alex Williamson
Quoted 53 lines · On Mon, 24 Feb 2020 14:42:17 +0800 "Longpeng(Mike)" <[email protected]>…

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +5 -3
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
     uint64_t data = 0;

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 873

Quoted 10 lines
hw/vfio/pci.c +3 -2
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,  

Looks like an obvious bug, until you look at the rest of this memcpy():

Quoted 8 lines
Quoted 7 lines

memcpy(&val, vdev->rom + addr,

           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

IOW, we'll do a zero sized memcpy() if rom_size is zero, so there's no risk of the concern identified in the commit log. This patch is unnecessary. Thanks,

Oh, I missed that, sorry for make the noise, thanks

Actually, not noise. After some internal discussion thanks to Laszlo, it seems that while memcpy() with a zero size seems to do the right thing, the behavior for any case where we pass a null pointer is not actually defined. However, there's still a bug in the implementation of the fix above, if vdev->rom_read_failed is set, we'll still fall through to the memcpy. I think there's also another bug in the current implementation that we initialize data to zero but we'll overwrite it with the uninitialized 'val' in the switch statement. I think the below resolves both. I'll formally post it after a bit of testing:

commit 2088fc1e1f426b98e9ca4d7bcdbe53d886a18c37

Author: Alex Williamson <[email protected]>
Date:   Tue Mar 10 10:04:36 2020 -0600

vfio/pci: Use defined memcpy() behavior

vfio_rom_read() relies on memcpy() doing the logically correct thing, ie. safely copying zero bytes from a NULL pointer when rom_size is zero, rather than the spec definition, which is undefined when the source or target pointers are NULL. Resolve this by wrapping the call in the condition expressed previously by the ternary.

Additionally, we still use @val to fill data based on the provided @size regardless of mempcy(), so we should initialize @val rather than @data.

hw/vfio/pci.c +5 -4
index 5e75a95129ac..b0799cdc28ad 100644
@@ -859,16 +859,17 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         uint16_t word;
         uint32_t dword;
         uint64_t qword;
-    } val;
-    uint64_t data = 0;
+    } val = { 0 };
+    uint64_t data;
 
     /* Load the ROM lazily when the guest tries to read it */
     if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
         vfio_pci_load_rom(vdev);
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
+    }
 
     switch (size) {
     case 1:
#5 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 36 lines · Alex Williamson

commit 2088fc1e1f426b98e9ca4d7bcdbe53d886a18c37

Author: Alex Williamson <[email protected]>
Date:   Tue Mar 10 10:04:36 2020 -0600

vfio/pci: Use defined memcpy() behavior

vfio_rom_read() relies on memcpy() doing the logically correct thing, ie. safely copying zero bytes from a NULL pointer when rom_size is zero, rather than the spec definition, which is undefined when the source or target pointers are NULL. Resolve this by wrapping the call in the condition expressed previously by the ternary.

Additionally, we still use @val to fill data based on the provided @size regardless of mempcy(), so we should initialize @val rather than @data.

hw/vfio/pci.c +2 -2
index 5e75a95129ac..b0799cdc28ad 100644
@@ -859,16 +859,17 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         uint16_t word;
         uint32_t dword;
         uint64_t qword;
-    } val;
-    uint64_t data = 0;
+    } val = { 0 };
+    uint64_t data;
 
     /* Load the ROM lazily when the guest tries to read it */
     if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
         vfio_pci_load_rom(vdev);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 875

Quoted 10 lines
hw/vfio/pci.c +3 -2
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
+    }
 
     switch (size) {
     case 1:

Regarding the pre-patch code:

My understanding is that the memcpy() could be reached with a guest-originated "addr" even if "vdev->rom" was NULL. If that's the case, then the pre-patch code invokes undefined behavior regardless of memcpy(), because it performs pointer arithmetic on a null pointer (not to mention that the type of that pointer is (void *)....)

Regarding the proposed change:

(addr < vdev->rom_size) requires that "vdev->rom_size" be positive. In that case, I assume that

- "vdev->rom" is not NULL, and
- MIN(size, vdev->rom_size - addr) bytes are "in range" for the object allocated at "vdev->rom".

So from a memcpy() and range perspective, the patch looks OK. But there's still a wart I dislike: we should never perform pointer arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or (unsigned char*) first.

Here's an excerpt from the ISO C99 standard:

-v-
6.5.6 Additive operators

Constraints

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have integer type. [...]
-^-

A "pointer-to-void" is not a "pointer to an object type", because "void" is not an object type -- it is an incomplete type that cannot be completed:

-v-
6.2.5 Types

1 [...] Types are partitioned into object types (types that fully
describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

[...]

19 The void type comprises an empty set of values; it is an incomplete
type that cannot be completed.
-^-

For a different illustration, (vdev->rom + addr) is equivalent to &(vdev->rom[addr]) -- and we clearly can't have an "array of void".

This anti-pattern (of doing pointer arithmetic on (void*)) likely comes from a guarantee that the standard does make, in the same "6.2.5 Types" section:

-v-
27 A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. 39) [...]

Footnote 39: The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
-^-

It does not extend to the "+" operator.

Thanks
Laszlo

#6 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 111 lines · On Wed, 11 Mar 2020 00:14:31 +0100 Laszlo Ersek <[email protected]> wrote:
Quoted 36 lines · Alex Williamson

commit 2088fc1e1f426b98e9ca4d7bcdbe53d886a18c37

Author: Alex Williamson <[email protected]>
Date:   Tue Mar 10 10:04:36 2020 -0600

vfio/pci: Use defined memcpy() behavior

vfio_rom_read() relies on memcpy() doing the logically correct thing, ie. safely copying zero bytes from a NULL pointer when rom_size is zero, rather than the spec definition, which is undefined when the source or target pointers are NULL. Resolve this by wrapping the call in the condition expressed previously by the ternary.

Additionally, we still use @val to fill data based on the provided @size regardless of mempcy(), so we should initialize @val rather than @data.

hw/vfio/pci.c +2 -2
index 5e75a95129ac..b0799cdc28ad 100644
@@ -859,16 +859,17 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         uint16_t word;
         uint32_t dword;
         uint64_t qword;
-    } val;
-    uint64_t data = 0;
+    } val = { 0 };
+    uint64_t data;
 
     /* Load the ROM lazily when the guest tries to read it */
     if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
         vfio_pci_load_rom(vdev);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 875

Quoted 10 lines
hw/vfio/pci.c +3 -2
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
+    }
 
     switch (size) {
     case 1:  

Regarding the pre-patch code:

My understanding is that the memcpy() could be reached with a guest-originated "addr" even if "vdev->rom" was NULL. If that's the case, then the pre-patch code invokes undefined behavior regardless of memcpy(), because it performs pointer arithmetic on a null pointer (not to mention that the type of that pointer is (void *)....)

Regarding the proposed change:

(addr < vdev->rom_size) requires that "vdev->rom_size" be positive. In that case, I assume that

- "vdev->rom" is not NULL, and
- MIN(size, vdev->rom_size - addr) bytes are "in range" for the object allocated at "vdev->rom".

So from a memcpy() and range perspective, the patch looks OK. But there's still a wart I dislike: we should never perform pointer arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or (unsigned char*) first.

Here's an excerpt from the ISO C99 standard:

-v-
6.5.6 Additive operators

Constraints

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have integer type. [...]
-^-

A "pointer-to-void" is not a "pointer to an object type", because "void" is not an object type -- it is an incomplete type that cannot be completed:

-v-
6.2.5 Types

1 [...] Types are partitioned into object types (types that fully
describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

[...]

19 The void type comprises an empty set of values; it is an incomplete
type that cannot be completed.
-^-

For a different illustration, (vdev->rom + addr) is equivalent to &(vdev->rom[addr]) -- and we clearly can't have an "array of void".

This anti-pattern (of doing pointer arithmetic on (void*)) likely comes from a guarantee that the standard does make, in the same "6.2.5 Types" section:

-v-
27 A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. 39) [...]

Quoted 7 lines

Footnote 39: The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
-^-

It does not extend to the "+" operator.

GNU C specifically allows arithmetic on pointers and defines the size of a void as 1. I'll comply, but this makes me want to stab myself in the face :-\ Thanks,

Alex

#7 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 2 lines · Alex Williamson

On Wed, 11 Mar 2020 00:14:31 +0100
Laszlo Ersek <[email protected]> wrote:

[...]

Quoted 51 lines
Quoted 51 lines

So from a memcpy() and range perspective, the patch looks OK. But there's still a wart I dislike: we should never perform pointer arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or (unsigned char*) first.

Here's an excerpt from the ISO C99 standard:

-v-
6.5.6 Additive operators

Constraints

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have integer type. [...]
-^-

A "pointer-to-void" is not a "pointer to an object type", because "void" is not an object type -- it is an incomplete type that cannot be completed:

-v-
6.2.5 Types

1 [...] Types are partitioned into object types (types that fully
describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

[...]

19 The void type comprises an empty set of values; it is an incomplete
type that cannot be completed.
-^-

For a different illustration, (vdev->rom + addr) is equivalent to &(vdev->rom[addr]) -- and we clearly can't have an "array of void".

This anti-pattern (of doing pointer arithmetic on (void*)) likely comes from a guarantee that the standard does make, in the same "6.2.5 Types" section:

-v-
27 A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. 39) [...]

Footnote 39: The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
-^-

Quoted 5 lines
Quoted 1 line

It does not extend to the "+" operator.

GNU C specifically allows arithmetic on pointers and defines the size of a void as 1. I'll comply, but this makes me want to stab myself in the face :-\ Thanks,

We rely on GNU C extensions all over theplace. Making the code uglier to avoid relying on this one here makes no sense to me.

Reviewed-by
#8 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 56 lines · Markus Armbruster
Quoted 2 lines · Alex Williamson

On Wed, 11 Mar 2020 00:14:31 +0100
Laszlo Ersek <[email protected]> wrote:

[...]

Quoted 51 lines
Quoted 51 lines

So from a memcpy() and range perspective, the patch looks OK. But there's still a wart I dislike: we should never perform pointer arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or (unsigned char*) first.

Here's an excerpt from the ISO C99 standard:

-v-
6.5.6 Additive operators

Constraints

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have integer type. [...]
-^-

A "pointer-to-void" is not a "pointer to an object type", because "void" is not an object type -- it is an incomplete type that cannot be completed:

-v-
6.2.5 Types

1 [...] Types are partitioned into object types (types that fully
describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

[...]

19 The void type comprises an empty set of values; it is an incomplete
type that cannot be completed.
-^-

For a different illustration, (vdev->rom + addr) is equivalent to &(vdev->rom[addr]) -- and we clearly can't have an "array of void".

This anti-pattern (of doing pointer arithmetic on (void*)) likely comes from a guarantee that the standard does make, in the same "6.2.5 Types" section:

-v-
27 A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. 39) [...]

Footnote 39: The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
-^-

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

It does not extend to the "+" operator.

GNU C specifically allows arithmetic on pointers and defines the size of a void as 1. I'll comply, but this makes me want to stab myself in the face :-\ Thanks,

We rely on GNU C extensions all over theplace. Making the code uglier to avoid relying on this one here makes no sense to me.

I agree, in fact. If GNU-isms are liberally used & tolerated in the QEMU source, then there's no reason to diverge from that here. I steer clear of GNU-isms as much as I can, regardless of codebase, but I *did* forget that QEMU permits GNU-isms -- so there's no need for my pedantry here.

Thanks!
Laszlo

#9 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

In reply to Alex Williamson
Quoted 120 lines · Alex Williamson
Quoted 117 lines · On Wed, 11 Mar 2020 00:14:31 +0100 Laszlo Ersek <[email protected]> wrote:
Quoted 36 lines · Alex Williamson

commit 2088fc1e1f426b98e9ca4d7bcdbe53d886a18c37

Author: Alex Williamson <[email protected]>
Date:   Tue Mar 10 10:04:36 2020 -0600

vfio/pci: Use defined memcpy() behavior

vfio_rom_read() relies on memcpy() doing the logically correct thing, ie. safely copying zero bytes from a NULL pointer when rom_size is zero, rather than the spec definition, which is undefined when the source or target pointers are NULL. Resolve this by wrapping the call in the condition expressed previously by the ternary.

Additionally, we still use @val to fill data based on the provided @size regardless of mempcy(), so we should initialize @val rather than @data.

hw/vfio/pci.c +2 -2
index 5e75a95129ac..b0799cdc28ad 100644
@@ -859,16 +859,17 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         uint16_t word;
         uint32_t dword;
         uint64_t qword;
-    } val;
-    uint64_t data = 0;
+    } val = { 0 };
+    uint64_t data;
 
     /* Load the ROM lazily when the guest tries to read it */
     if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
         vfio_pci_load_rom(vdev);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 875

Quoted 10 lines
hw/vfio/pci.c +3 -2
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
+    }
 
     switch (size) {
     case 1:  

Regarding the pre-patch code:

My understanding is that the memcpy() could be reached with a guest-originated "addr" even if "vdev->rom" was NULL. If that's the case, then the pre-patch code invokes undefined behavior regardless of memcpy(), because it performs pointer arithmetic on a null pointer (not to mention that the type of that pointer is (void *)....)

Regarding the proposed change:

(addr < vdev->rom_size) requires that "vdev->rom_size" be positive. In that case, I assume that

- "vdev->rom" is not NULL, and
- MIN(size, vdev->rom_size - addr) bytes are "in range" for the object allocated at "vdev->rom".

So from a memcpy() and range perspective, the patch looks OK. But there's still a wart I dislike: we should never perform pointer arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or (unsigned char*) first.

Here's an excerpt from the ISO C99 standard:

-v-
6.5.6 Additive operators

Constraints

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have integer type. [...]
-^-

A "pointer-to-void" is not a "pointer to an object type", because "void" is not an object type -- it is an incomplete type that cannot be completed:

-v-
6.2.5 Types

1 [...] Types are partitioned into object types (types that fully
describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

[...]

19 The void type comprises an empty set of values; it is an incomplete
type that cannot be completed.
-^-

For a different illustration, (vdev->rom + addr) is equivalent to &(vdev->rom[addr]) -- and we clearly can't have an "array of void".

This anti-pattern (of doing pointer arithmetic on (void*)) likely comes from a guarantee that the standard does make, in the same "6.2.5 Types" section:

-v-
27 A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. 39) [...]

Footnote 39: The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
-^-

Quoted 5 lines
Quoted 1 line

It does not extend to the "+" operator.

GNU C specifically allows arithmetic on pointers and defines the size of a void as 1. I'll comply, but this makes me want to stab myself in the face :-\ Thanks,

Sorry, I didn't want to annoy you. :)

In fact I was about to mention, "I really don't understand why compilers don't yell upon seeing pointer-to-void arithmetic", but I got distracted and forgot about that thought. In retrospect, that may have been for the best! :)

Thanks
Laszlo

#10 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 127 lines · Laszlo Ersek
Quoted 120 lines · Alex Williamson
Quoted 117 lines · On Wed, 11 Mar 2020 00:14:31 +0100 Laszlo Ersek <[email protected]> wrote:
Quoted 36 lines · Alex Williamson

commit 2088fc1e1f426b98e9ca4d7bcdbe53d886a18c37

Author: Alex Williamson <[email protected]>
Date:   Tue Mar 10 10:04:36 2020 -0600

vfio/pci: Use defined memcpy() behavior

vfio_rom_read() relies on memcpy() doing the logically correct thing, ie. safely copying zero bytes from a NULL pointer when rom_size is zero, rather than the spec definition, which is undefined when the source or target pointers are NULL. Resolve this by wrapping the call in the condition expressed previously by the ternary.

Additionally, we still use @val to fill data based on the provided @size regardless of mempcy(), so we should initialize @val rather than @data.

hw/vfio/pci.c +2 -2
index 5e75a95129ac..b0799cdc28ad 100644
@@ -859,16 +859,17 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         uint16_t word;
         uint32_t dword;
         uint64_t qword;
-    } val;
-    uint64_t data = 0;
+    } val = { 0 };
+    uint64_t data;
 
     /* Load the ROM lazily when the guest tries to read it */
     if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
         vfio_pci_load_rom(vdev);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 875

Quoted 10 lines
hw/vfio/pci.c +3 -2
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
+    }
 
     switch (size) {
     case 1:  

Regarding the pre-patch code:

My understanding is that the memcpy() could be reached with a guest-originated "addr" even if "vdev->rom" was NULL. If that's the case, then the pre-patch code invokes undefined behavior regardless of memcpy(), because it performs pointer arithmetic on a null pointer (not to mention that the type of that pointer is (void *)....)

Regarding the proposed change:

(addr < vdev->rom_size) requires that "vdev->rom_size" be positive. In that case, I assume that

- "vdev->rom" is not NULL, and
- MIN(size, vdev->rom_size - addr) bytes are "in range" for the object allocated at "vdev->rom".

So from a memcpy() and range perspective, the patch looks OK. But there's still a wart I dislike: we should never perform pointer arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or (unsigned char*) first.

Here's an excerpt from the ISO C99 standard:

-v-
6.5.6 Additive operators

Constraints

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have integer type. [...]
-^-

A "pointer-to-void" is not a "pointer to an object type", because "void" is not an object type -- it is an incomplete type that cannot be completed:

-v-
6.2.5 Types

1 [...] Types are partitioned into object types (types that fully
describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

[...]

19 The void type comprises an empty set of values; it is an incomplete
type that cannot be completed.
-^-

For a different illustration, (vdev->rom + addr) is equivalent to &(vdev->rom[addr]) -- and we clearly can't have an "array of void".

This anti-pattern (of doing pointer arithmetic on (void*)) likely comes from a guarantee that the standard does make, in the same "6.2.5 Types" section:

-v-
27 A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. 39) [...]

Footnote 39: The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
-^-

Quoted 5 lines
Quoted 1 line

It does not extend to the "+" operator.

GNU C specifically allows arithmetic on pointers and defines the size of a void as 1. I'll comply, but this makes me want to stab myself in the face :-\ Thanks,

Quoted 6 lines

Sorry, I didn't want to annoy you. :)

In fact I was about to mention, "I really don't understand why compilers don't yell upon seeing pointer-to-void arithmetic", but I got distracted and forgot about that thought. In retrospect, that may have been for the best! :)

You're looking for

'-Wpointer-arith'
Warn about anything that depends on the "size of" a function type or of 'void'. GNU C assigns these types a size of 1, for convenience in calculations with 'void *' pointers and pointers to functions. In C++, warn also when an arithmetic operation involves 'NULL'. This warning is also enabled by '-Wpedantic'.

#11 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 7 lines · Markus Armbruster
Quoted 4 lines · Laszlo Ersek

In fact I was about to mention, "I really don't understand why compilers don't yell upon seeing pointer-to-void arithmetic", but I got distracted and forgot about that thought. In retrospect, that may have been for the best! :)

Quoted 9 lines

You're looking for

'-Wpointer-arith'
Warn about anything that depends on the "size of" a function type or of 'void'. GNU C assigns these types a size of 1, for convenience in calculations with 'void *' pointers and pointers to functions. In C++, warn also when an arithmetic operation involves 'NULL'. This warning is also enabled by '-Wpedantic'.

Thanks! It seems like "-Wpedantic" and "-pedantic" are synonymous. And the latter used to be part of my standard set of flags, while I worked on hosted C programs where I could influence the build flags ;)

Cheers,
Laszlo

#12 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

In reply to Alex Williamson
Quoted 8 lines · Alex Williamson
Quoted 5 lines · On Mon, 24 Feb 2020 14:42:17 +0800 "Longpeng(Mike)" <[email protected]>…

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

What "VM abort" exactly?

Quoted 45 lines
Quoted 45 lines
---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +5 -3
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
Quoted 2 lines

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)

Quoted 2 lines
hw/vfio/pci.c +0 -0
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
    {
        VFIOPCIDevice *vdev = opaque;
        union {
            uint8_t byte;
            uint16_t word;
            uint32_t dword;
            uint64_t qword;
        } val;
Quoted 12 lines

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 873

Quoted 11 lines
hw/vfio/pci.c +3 -2
     uint64_t data = 0;
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,
Quoted 8 lines

Looks like an obvious bug, until you look at the rest of this memcpy():

memcpy(&val, vdev->rom + addr,

           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

IOW, we'll do a zero sized memcpy() if rom_size is zero, so there's no risk of the concern identified in the commit log. This patch is unnecessary. Thanks,

I'm blind: why does !vdev->rom imply !vdev->rom_size?

Moreover, when MIN(size, vdev->rom_size - addr) < size, we seem to read uninitialized data from @val:

        switch (size) {
        case 1:
            data = val.byte;
            break;
        case 2:
            data = le16_to_cpu(val.word);
            break;
        case 4:
            data = le32_to_cpu(val.dword);
            break;
        default:
            hw_error("vfio: unsupported read size, %d bytes\n", size);
            break;
        }

        trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);

        return data;
    }

Why is that okay?

Why do we initialize @data?

How can we get to the default case? If we can get there, is hw_error() really the right thing to do? It almost never is... If getting there is the guest's fault, we need to tell it off the same way physical hardware does. If we should not ever get there (i.e. it's a QEMU bug), then a plain abort() would be clearer.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 91 lines · Alex Williamson
Quoted 5 lines · On Wed, 11 Mar 2020 08:04:28 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 3 lines · Alex Williamson

On Mon, 24 Feb 2020 14:42:17 +0800
"Longpeng(Mike)" <[email protected]> wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

What "VM abort" exactly?

There is none because memcpy() does something sane when size is zero, but to be ISO whatever spec compliant we shouldn't rely on that.

Quoted 72 lines
Quoted 45 lines
Quoted 45 lines
---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +5 -3
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
Quoted 2 lines

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)

Quoted 2 lines
hw/vfio/pci.c +0 -0
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)  
    {
        VFIOPCIDevice *vdev = opaque;
        union {
            uint8_t byte;
            uint16_t word;
            uint32_t dword;
            uint64_t qword;
        } val;
Quoted 17 lines

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 873

Quoted 11 lines
hw/vfio/pci.c +3 -2
     uint64_t data = 0;
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,  

Looks like an obvious bug, until you look at the rest of this memcpy():

memcpy(&val, vdev->rom + addr,

           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

IOW, we'll do a zero sized memcpy() if rom_size is zero, so there's no risk of the concern identified in the commit log. This patch is unnecessary. Thanks,

I'm blind: why does !vdev->rom imply !vdev->rom_size?

See vfio_pci_load_rom(), rom_size and rom are set and allocated together.

What if vfio_pci_load_rom() isn't called, or returns before it sets these guys?

Quoted 5 lines
Quoted 2 lines

Moreover, when MIN(size, vdev->rom_size - addr) < size, we seem to read uninitialized data from @val:

This is fixed in my patch
https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg02778.html

Yes.

Quoted 38 lines
Quoted 21 lines
        switch (size) {
        case 1:
            data = val.byte;
            break;
        case 2:
            data = le16_to_cpu(val.word);
            break;
        case 4:
            data = le32_to_cpu(val.dword);
            break;
        default:
            hw_error("vfio: unsupported read size, %d bytes\n", size);
            break;
        }

        trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);

        return data;
    }
Quoted 3 lines

Why is that okay?

Why do we initialize @data?

Bug. The switch was only added later (75bd0c7253f3) and we failed to catch it. Prior to that we were initializing val and the memcpy() only overwrote it as necessary. In any case, getting back garbage for the rom when there isn't one generally works ok since the chances of generating a proper rom signature are infinitesimal. Clearly not what was intended though.

Quoted 5 lines

How can we get to the default case? If we can get there, is hw_error() really the right thing to do? It almost never is... If getting there is the guest's fault, we need to tell it off the same way physical hardware does. If we should not ever get there (i.e. it's a QEMU bug), then a plain abort() would be clearer.

Quoted 5 lines

AFAIK this is relatively standard, if not somewhat paranoid, handling for a MemoryRegion ops callback. The MemoryRegionOps code only allows certain size accesses, so it would effectively be an internal error to hit the default case, which seems to be not an uncommon use case of hw_error. Thanks,

Using hw_error() for such programming errors is not helpful. Everything it adds to abort() is useless or misleading.

In fact, most uses of hw_error() are not helpful.

But you're going with the flow here. I accept that.

#15 lore raw

Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read

Quoted 97 lines · On Thu, 12 Mar 2020 06:50:30 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 95 lines · Alex Williamson
Quoted 5 lines · On Wed, 11 Mar 2020 08:04:28 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 3 lines · Alex Williamson

On Mon, 24 Feb 2020 14:42:17 +0800
"Longpeng(Mike)" <[email protected]> wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

From: Longpeng <[email protected]>

vfio_pci_load_rom() maybe failed and then the vdev->rom is NULL in some situation (though I've not encountered yet), maybe we should avoid the VM abort.

What "VM abort" exactly?

There is none because memcpy() does something sane when size is zero, but to be ISO whatever spec compliant we shouldn't rely on that.

Quoted 76 lines
Quoted 45 lines
Quoted 45 lines
---
 hw/vfio/pci.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/vfio/pci.c +5 -3
index 5e75a95..ed798ae 100644
@@ -768,7 +768,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     struct vfio_region_info *reg_info;
     uint64_t size;
@@ -778,7 +778,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     if (vfio_get_region_info(&vdev->vbasedev,
                              VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
         error_report("vfio: Error getting ROM info: %m");
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
@@ -797,7 +797,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         error_printf("Device option ROM contents are probably invalid "
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
                     "or load from file with romfile=\n");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -849,6 +849,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
Quoted 2 lines

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)

Quoted 2 lines
hw/vfio/pci.c +0 -0
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -863,8 +865,9 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)    
    {
        VFIOPCIDevice *vdev = opaque;
        union {
            uint8_t byte;
            uint16_t word;
            uint32_t dword;
            uint64_t qword;
        } val;  
Quoted 20 lines

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 873

Quoted 11 lines
hw/vfio/pci.c +3 -2
     uint64_t data = 0;
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vfio_pci_load_rom(vdev);
+    if (unlikely(!vdev->rom && !vdev->rom_read_failed) &&
+        !vfio_pci_load_rom(vdev)) {
+        return 0;
     }
 
     memcpy(&val, vdev->rom + addr,    

Looks like an obvious bug, until you look at the rest of this memcpy():

memcpy(&val, vdev->rom + addr,

           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

IOW, we'll do a zero sized memcpy() if rom_size is zero, so there's no risk of the concern identified in the commit log. This patch is unnecessary. Thanks,

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I'm blind: why does !vdev->rom imply !vdev->rom_size?

See vfio_pci_load_rom(), rom_size and rom are set and allocated together.

What if vfio_pci_load_rom() isn't called, or returns before it sets these guys?

vfio_pci_load_rom() is called from this read function if they're not set and we haven't triggered a read failure. They're zero allocated. The intention is that any vfio_pci_load_rom() failure will leave these unset and trigger a zero sized memcpy, or now skip the mempcy as in the patch below. Thanks,

Alex

Quoted 58 lines
Quoted 5 lines
Quoted 2 lines

Moreover, when MIN(size, vdev->rom_size - addr) < size, we seem to read uninitialized data from @val:

This is fixed in my patch
https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg02778.html

Yes.

Quoted 38 lines
Quoted 21 lines
        switch (size) {
        case 1:
            data = val.byte;
            break;
        case 2:
            data = le16_to_cpu(val.word);
            break;
        case 4:
            data = le32_to_cpu(val.dword);
            break;
        default:
            hw_error("vfio: unsupported read size, %d bytes\n", size);
            break;
        }

        trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);

        return data;
    }
Quoted 3 lines

Why is that okay?

Why do we initialize @data?

Bug. The switch was only added later (75bd0c7253f3) and we failed to catch it. Prior to that we were initializing val and the memcpy() only overwrote it as necessary. In any case, getting back garbage for the rom when there isn't one generally works ok since the chances of generating a proper rom signature are infinitesimal. Clearly not what was intended though.

Quoted 5 lines

How can we get to the default case? If we can get there, is hw_error() really the right thing to do? It almost never is... If getting there is the guest's fault, we need to tell it off the same way physical hardware does. If we should not ever get there (i.e. it's a QEMU bug), then a plain abort() would be clearer.

Quoted 5 lines

AFAIK this is relatively standard, if not somewhat paranoid, handling for a MemoryRegion ops callback. The MemoryRegionOps code only allows certain size accesses, so it would effectively be an internal error to hit the default case, which seems to be not an uncommon use case of hw_error. Thanks,

Using hw_error() for such programming errors is not helpful. Everything it adds to abort() is useless or misleading.

In fact, most uses of hw_error() are not helpful.

But you're going with the flow here. I accept that.

#16 lore raw

PATCH RESEND 2/3 vhost: fix a null pointer reference of vhost_log

In reply to Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

---
 hw/virtio/vhost.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
hw/virtio/vhost.c +17 -2
index 9edfadc..c7ad6e5 100644
@@ -219,6 +219,10 @@ static struct vhost_log *vhost_log_get(uint64_t size, bool share)
 
     if (!log || log->size != size) {
         log = vhost_log_alloc(size, share);
+        if (!log) {
+            return NULL;
+        }
+
         if (share) {
             vhost_log_shm = log;
         } else {
@@ -270,10 +274,17 @@ static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
 
 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
 {
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+
+    log_base = (uintptr_t)log->log;
+
     /* inform backend of log switching, this must be done before
        releasing the current log, to ensure no logging is lost */
     r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
@@ -1640,6 +1651,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
         hdev->log_size = vhost_get_log_size(hdev);
         hdev->log = vhost_log_get(hdev->log_size,
                                   vhost_dev_log_is_shared(hdev));
+        if (!hdev->log) {
+            goto fail_vq;
+        }
+
         log_base = (uintptr_t)hdev->log->log;
         r = hdev->vhost_ops->vhost_set_log_base(hdev,
                                                 hdev->log_size ? log_base : 0,
Signature · 1 line
1.8.3.1
#17 lore raw

Re: [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log

Hi Michael,

ping...

Quoted 58 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

---
 hw/virtio/vhost.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
hw/virtio/vhost.c +17 -2
index 9edfadc..c7ad6e5 100644
@@ -219,6 +219,10 @@ static struct vhost_log *vhost_log_get(uint64_t size, bool share)
 
     if (!log || log->size != size) {
         log = vhost_log_alloc(size, share);
+        if (!log) {
+            return NULL;
+        }
+
         if (share) {
             vhost_log_shm = log;
         } else {
@@ -270,10 +274,17 @@ static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
 
 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
 {
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+
+    log_base = (uintptr_t)log->log;
+
     /* inform backend of log switching, this must be done before
        releasing the current log, to ensure no logging is lost */
     r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
@@ -1640,6 +1651,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
         hdev->log_size = vhost_get_log_size(hdev);
         hdev->log = vhost_log_get(hdev->log_size,
                                   vhost_dev_log_is_shared(hdev));
+        if (!hdev->log) {
+            goto fail_vq;
+        }
+
         log_base = (uintptr_t)hdev->log->log;
         r = hdev->vhost_ops->vhost_set_log_base(hdev,
                                                 hdev->log_size ? log_base : 0,
Signature · 3 lines
---
Regards,
Longpeng(Mike)
#18 lore raw

Re: [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log

In reply to Longpeng(Mike)
Quoted 31 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

---
 hw/virtio/vhost.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
hw/virtio/vhost.c +4 -1
index 9edfadc..c7ad6e5 100644
@@ -219,6 +219,10 @@ static struct vhost_log *vhost_log_get(uint64_t size, bool share)
 
     if (!log || log->size != size) {
         log = vhost_log_alloc(size, share);
+        if (!log) {
+            return NULL;
+        }
+
         if (share) {
             vhost_log_shm = log;
         } else {
@@ -270,10 +274,17 @@ static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
 
 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
 {
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));

hw/virtio/vhost.c static bool vhost_dev_log_is_shared(struct vhost_dev *dev) · line 285

Quoted 10 lines
hw/virtio/vhost.c +7 -1
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+

I'm not sure silently failing like this is safe. Callers assume log can be resized. What can be done? I suspect not much beside exiting ...
Speaking of which, lots of other failures in log resizing path seem to be silently ignored.
I guess we should propagate them, and fix callers to check the return code?

#19 lore raw

Re: [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log

Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

---
 hw/virtio/vhost.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
hw/virtio/vhost.c +6 -2
index 9edfadc..c7ad6e5 100644
@@ -219,6 +219,10 @@ static struct vhost_log *vhost_log_get(uint64_t size, bool share)
 
     if (!log || log->size != size) {
         log = vhost_log_alloc(size, share);
+        if (!log) {
+            return NULL;
+        }
+
         if (share) {
             vhost_log_shm = log;
         } else {
@@ -270,10 +274,17 @@ static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
 
 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
 {
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
Quoted 14 lines

hw/virtio/vhost.c static bool vhost_dev_log_is_shared(struct vhost_dev *dev) · line 285

Quoted 5 lines
hw/virtio/vhost.c +5 -0
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+

I'm not sure silently failing like this is safe. Callers assume log can be resized. What can be done? I suspect not much beside exiting ...
Speaking of which, lots of other failures in log resizing path seem to be silently ignored.
I guess we should propagate them, and fix callers to check the return code?

How about to let the callers treat the failure of log_resize as a fatal error ?

-static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) +static inline int vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
{

-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;

+ log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+ if (!log) {
+ r = -1;
+ goto out;
+ }
+
+ log_base = (uintptr_t)log->log;
+

     /* inform backend of log switching, this must be done before
        releasing the current log, to ensure no logging is lost */
     r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
@@ -284,6 +296,9 @@ static inline void vhost_dev_log_resize(struct vhost_dev
*dev, uint64_t size)
     vhost_log_put(dev, true);
     dev->log = log;
     dev->log_size = size;

+
+out:
+ return 0;
}

@@ -510,7 +525,9 @@ static void vhost_commit(MemoryListener *listener)

 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
     /* To log more, must increase log size before table update. */
     if (dev->log_size < log_size) {

- vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
+ if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+ abort();
+ }

     }
     r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
     if (r < 0) {
@@ -518,7 +535,9 @@ static void vhost_commit(MemoryListener *listener)
     }
     /* To log less, can only decrease log size after table update. */
     if (dev->log_size > log_size + VHOST_LOG_BUFFER) {

- vhost_dev_log_resize(dev, log_size);
+ if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+ abort();
+ }

     }

out:
@@ -818,7 +837,11 @@ static int vhost_migration_log(MemoryListener *listener, int enable)

         }
         vhost_log_put(dev, false);
     } else {
-        vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        r = vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        if (r < 0) {
+            return r;
+        }
+
         r = vhost_dev_set_log(dev, true);
         if (r < 0) {
             return r;
Quoted 3 lines

.

Signature · 3 lines
---
Regards,
Longpeng(Mike)
#20 lore raw

Re: [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log

Quoted 99 lines · Cloud Infrastructure Service Product Dept.)
Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

---
 hw/virtio/vhost.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
hw/virtio/vhost.c +6 -2
index 9edfadc..c7ad6e5 100644
@@ -219,6 +219,10 @@ static struct vhost_log *vhost_log_get(uint64_t size, bool share)
 
     if (!log || log->size != size) {
         log = vhost_log_alloc(size, share);
+        if (!log) {
+            return NULL;
+        }
+
         if (share) {
             vhost_log_shm = log;
         } else {
@@ -270,10 +274,17 @@ static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
 
 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
 {
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
Quoted 14 lines

hw/virtio/vhost.c static bool vhost_dev_log_is_shared(struct vhost_dev *dev) · line 285

Quoted 5 lines
hw/virtio/vhost.c +5 -0
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+

I'm not sure silently failing like this is safe. Callers assume log can be resized. What can be done? I suspect not much beside exiting ...
Speaking of which, lots of other failures in log resizing path seem to be silently ignored.
I guess we should propagate them, and fix callers to check the return code?

How about to let the callers treat the failure of log_resize as a fatal error ?

hw/virtio/vhost.c +17 -4
-static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
+static inline int vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
 {
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        r = -1;
+        goto out;
+    }
+
+    log_base = (uintptr_t)log->log;
+
     /* inform backend of log switching, this must be done before
        releasing the current log, to ensure no logging is lost */
     r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
@@ -284,6 +296,9 @@ static inline void vhost_dev_log_resize(struct vhost_dev
 *dev, uint64_t size)
     vhost_log_put(dev, true);
     dev->log = log;
     dev->log_size = size;
+
+out:
+    return 0;
 }
 
@@ -510,7 +525,9 @@ static void vhost_commit(MemoryListener *listener)
 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
     /* To log more, must increase log size before table update. */
     if (dev->log_size < log_size) {
-        vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
+        if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+            abort();
+        }
     }
     r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
     if (r < 0) {

hw/virtio/vhost.c static void vhost_commit(MemoryListener *listener) · line 543

Quoted 10 lines
hw/virtio/vhost.c +3 -1
@@ -518,7 +535,9 @@ static void vhost_commit(MemoryListener *listener)
     }
     /* To log less, can only decrease log size after table update. */
     if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
-        vhost_dev_log_resize(dev, log_size);
+        if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+            abort();
+        }
     }
 
 out:

I think the suggested handling is
error_report() and exit().
we also need to propagate errno. So how about passing in Error then?

Quoted 24 lines
(quoted diff) +5 -1
@@ -818,7 +837,11 @@ static int vhost_migration_log(MemoryListener *listener,
 int enable)
         }
         vhost_log_put(dev, false);
     } else {
-        vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        r = vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        if (r < 0) {
+            return r;
+        }
+
         r = vhost_dev_set_log(dev, true);
         if (r < 0) {
             return r;
Quoted 3 lines

.

Signature · 3 lines
---
Regards,
Longpeng(Mike)
#21 lore raw

Re: [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log

Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Cloud Infrastructure Service Product Dept.)
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

[...]

Quoted 24 lines
Quoted 24 lines
Quoted 14 lines

hw/virtio/vhost.c static bool vhost_dev_log_is_shared(struct vhost_dev *dev) · line 285

Quoted 13 lines

static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) {

hw/virtio/vhost.c +7 -2
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+
Quoted 8 lines

I'm not sure silently failing like this is safe. Callers assume log can be resized. What can be done? I suspect not much beside exiting ...
Speaking of which, lots of other failures in log resizing path seem to be silently ignored.
I guess we should propagate them, and fix callers to check the return code?

How about to let the callers treat the failure of log_resize as a fatal error ?

[...]

Quoted 22 lines
Quoted 22 lines
(quoted diff) +6 -2
@@ -510,7 +525,9 @@ static void vhost_commit(MemoryListener *listener)
 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
     /* To log more, must increase log size before table update. */
     if (dev->log_size < log_size) {
-        vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
+        if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+            abort();
+        }
     }
     r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
     if (r < 0) {
@@ -518,7 +535,9 @@ static void vhost_commit(MemoryListener *listener)
     }
     /* To log less, can only decrease log size after table update. */
     if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
-        vhost_dev_log_resize(dev, log_size);
+        if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+            abort();
+        }
     }
 
Quoted 7 lines

Review static void vhost_commit(MemoryListener *listener) · line 543

Quoted 1 line
(quoted diff) +0 -0
 out:

I think the suggested handling is
error_report() and exit().
we also need to propagate errno. So how about passing in Error then?

vhost_dev_log_resize
vhost_log_get
vhost_log_alloc
error_report_err (fail path, errno is in the errp)
VHOST_OPS_DEBUG (if ->vhost_set_log_base fail)
error_report (errno)

Um, it seems log_resize will report error with errno internal, do we need error_report once more ?

Quoted 29 lines
Quoted 18 lines
(quoted diff) +5 -1
@@ -818,7 +837,11 @@ static int vhost_migration_log(MemoryListener *listener,
 int enable)
         }
         vhost_log_put(dev, false);
     } else {
-        vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        r = vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        if (r < 0) {
+            return r;
+        }
+
         r = vhost_dev_set_log(dev, true);
         if (r < 0) {
             return r;
Quoted 1 line
Quoted 7 lines
Quoted 2 lines

.

Signature · 3 lines
---
Regards,
Longpeng(Mike)

.

Signature · 2 lines
Regards,
Longpeng(Mike)
#22 lore raw

Re: [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log

Quoted 76 lines · Longpeng (Mike)
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Cloud Infrastructure Service Product Dept.)
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

vhost_log_alloc() may fails and returned pointer of log is null. However there're two places derefernce the return pointer without check.

[...]

Quoted 24 lines
Quoted 24 lines
Quoted 14 lines

hw/virtio/vhost.c static bool vhost_dev_log_is_shared(struct vhost_dev *dev) · line 285

Quoted 13 lines

static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) {

hw/virtio/vhost.c +7 -2
-    struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
-    uint64_t log_base = (uintptr_t)log->log;
+    struct vhost_log *log;
+    uint64_t log_base;
     int r;
 
+    log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
+    if (!log) {
+        return;
+    }
+
Quoted 8 lines

I'm not sure silently failing like this is safe. Callers assume log can be resized. What can be done? I suspect not much beside exiting ...
Speaking of which, lots of other failures in log resizing path seem to be silently ignored.
I guess we should propagate them, and fix callers to check the return code?

How about to let the callers treat the failure of log_resize as a fatal error ?

[...]

Quoted 22 lines
Quoted 22 lines
(quoted diff) +6 -2
@@ -510,7 +525,9 @@ static void vhost_commit(MemoryListener *listener)
 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
     /* To log more, must increase log size before table update. */
     if (dev->log_size < log_size) {
-        vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
+        if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+            abort();
+        }
     }
     r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
     if (r < 0) {
@@ -518,7 +535,9 @@ static void vhost_commit(MemoryListener *listener)
     }
     /* To log less, can only decrease log size after table update. */
     if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
-        vhost_dev_log_resize(dev, log_size);
+        if (vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER) < 0) {
+            abort();
+        }
     }
 
Quoted 7 lines

Review static void vhost_commit(MemoryListener *listener) · line 543

Quoted 1 line
(quoted diff) +0 -0
 out:

I think the suggested handling is
error_report() and exit().
we also need to propagate errno. So how about passing in Error then?

vhost_dev_log_resize
vhost_log_get
vhost_log_alloc
error_report_err (fail path, errno is in the errp)
VHOST_OPS_DEBUG (if ->vhost_set_log_base fail)
error_report (errno)

Quoted 2 lines

Um, it seems log_resize will report error with errno internal, do we need error_report once more ?

Well we need to convert this over to something other than VHOST_OPS_DEBUG, that will go away at some point.

Quoted 34 lines
Quoted 29 lines
Quoted 18 lines
(quoted diff) +5 -1
@@ -818,7 +837,11 @@ static int vhost_migration_log(MemoryListener *listener,
 int enable)
         }
         vhost_log_put(dev, false);
     } else {
-        vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        r = vhost_dev_log_resize(dev, vhost_get_log_size(dev));
+        if (r < 0) {
+            return r;
+        }
+
         r = vhost_dev_set_log(dev, true);
         if (r < 0) {
             return r;
Quoted 1 line
Quoted 7 lines
Quoted 2 lines

.

Signature · 3 lines
---
Regards,
Longpeng(Mike)

.

Signature · 2 lines
Regards,
Longpeng(Mike)
#23 lore raw

PATCH RESEND 3/3 util/pty: fix a null pointer reference in qemu_openpty_raw

In reply to Longpeng(Mike)

From: Longpeng <[email protected]>

q_ptsname may failed ane return null, so use the returned pointer as the param of strcpy will cause null pointer deference. Use the return string of openpty instead of call ptsname.

---
 util/qemu-openpty.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
util/qemu-openpty.c +2 -8
index 2e8b43b..2bea4ba 100644
@@ -112,13 +112,7 @@ int qemu_openpty_raw(int *aslave, char *pty_name)
 {
     int amaster;
     struct termios tty;
-#if defined(__OpenBSD__) || defined(__DragonFly__)
-    char pty_buf[PATH_MAX];
-#define q_ptsname(x) pty_buf
-#else
-    char *pty_buf = NULL;
-#define q_ptsname(x) ptsname(x)
-#endif
+    char pty_buf[PATH_MAX] = { 0 };
 
     if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) {
         return -1;
@@ -130,7 +124,7 @@ int qemu_openpty_raw(int *aslave, char *pty_name)
     tcsetattr(*aslave, TCSAFLUSH, &tty);
 
     if (pty_name) {
-        strcpy(pty_name, q_ptsname(amaster));
+        strcpy(pty_name, pty_buf);
     }
 
     return amaster;
Signature · 1 line
1.8.3.1