vfio/pci: Reject invalid PCI interrupt pin values
PATCH 1/1 vfio/pci: Reject invalid PCI interrupt pin values
If a passthrough device's config space is unreadable at realize time (wedged link, surprise removal, powered-off GPU, etc.), the read of PCI_INTERRUPT_PIN comes back as 0xff. That value is non-zero, so it passes the "!pin" early-return check, and vfio_intx_enable() turns it into an out-of-range intx.pin (254). That value survives into pci_irq_deassert() -> pci_set_irq() -> pci_irq_handler(), which asserts 0 <= irq_num < PCI_NUM_PINS and aborts, killing the whole qemu-kvm process with SIGABRT instead of just failing the device.
Reject pin values outside the legal PCI_INTERRUPT_PIN range so this now surfaces as an ordinary device realize failure.
--- hw/vfio/pci.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/vfio/pci.c +6 -0
@@ -812,6 +812,12 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)PCIDevice *pdev =PCI_DEVICE (vdev);int ret; +/* pin reads back as 0xff if the device is unresponsive */ +if (pin >PCI_NUM_PINS ) { +error_setg (errp,"Invalid PCI interrupt pin %d" , pin); +return -EINVAL ; + } +vfio_disable_interrupts (vdev); vdev->msi_vectors =g_new0 (VFIOMSIVector, vdev->msix->entries);
Signature · 1 line
2.53.0
Quoted 29 lines · Denis V. Lunev
This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive. If a passthrough device's config space is unreadable at realize
time (wedged link, surprise removal, powered-off GPU, etc.), the
read ofPCI_INTERRUPT_PINcomes back as0xff. That value is
non-zero, so it passes the "!pin" early-return check, andvfio_intx_enable()turns it into an out-of-range intx.pin (254).
That value survives intopci_irq_deassert()->pci_set_irq()->pci_irq_handler(), which asserts 0 <=irq_num<PCI_NUM_PINSand
aborts, killing the whole qemu-kvm process with SIGABRT instead
of just failing the device.Reject pin values outside the legal
PCI_INTERRUPT_PINrange so this now surfaces as an ordinary device realize failure.Signed-off-by Denis V. Lunev <[email protected]>CC Alex Williamson <[email protected]>CC "Cedric Le Goater" <[email protected]>--- hw/vfio/pci.c | 6 ++++++ 1 file changed, 6 insertions(+)hw/vfio/pci.c +0
-0@@ -812,6 +812,12 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)PCIDevice *pdev =PCI_DEVICE (vdev);int ret;
hw/vfio/pci.c static void vfio_msix_enable(VFIOPCIDevice *vdev) · line 823
Quoted 10 lines
hw/vfio/pci.c +6
-0+/* pin reads back as 0xff if the device is unresponsive */ +if (pin >PCI_NUM_PINS ) { +error_setg (errp,"Invalid PCI interrupt pin %d" , pin); +return -EINVAL ; + } +vfio_disable_interrupts (vdev); vdev->msi_vectors =g_new0 (VFIOMSIVector, vdev->msix->entries);
pls disregard. Completely wrong port from downstream.
Sorry,
Den
v2 cover vfio/pci: reject invalid PCI_INTERRUPT_PIN values
In reply to Denis V. Lunevqemu-kvm aborts a few seconds after starting a VM with a passed-through GPU whose PCI_INTERRUPT_PIN comes back as an out-of-range value: vfio_intx_enable() only guards against pin == 0 and stores vdev->intx.pin = pin - 1 with no upper-bound check. That value reaches pci_irq_handler()'sassert(0 <= irq_num && irq_num < PCI_NUM_PINS) via pci_irq_deassert() -> pci_set_irq(), aborting the whole process instead of just failing the device.
v1 -> v2:
* original submission was wrongly ported patch from downstream. Fixed
* added checks for narrowed values
Denis V. Lunev (2):
vfio/pci: don't narrow a failed config read to a plausible value vfio/pci: reject invalid PCI_INTERRUPT_PIN values
hw/vfio/pci.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 vfio/pci: don't narrow a failed config read to a plausible value
In reply to Denis V. Lunevvfio_pci_read_config() signals a failed host-side read by returning (uint32_t)-1, regardless of the requested length. vfio_intx_enable() and vfio_pci_pre_reset() both narrowed that return value straight into a uint8_t/uint16_t local before checking anything, which truncates -1 into 0xff or 0xffff - values a real 1- or 2-byte register read can legitimately produce. From that point on, a failed read and real all-ones content are indistinguishable.
Keep the full uint32_t result and check it against (uint32_t)-1 before narrowing. In vfio_pci_pre_reset(), skip the corresponding write-back on a failed read instead of writing back constructed garbage to the device.
---
hw/vfio/pci.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-)
hw/vfio/pci.c +25 -7
@@ -323,10 +323,16 @@ static void vfio_irqchip_change(Notifier *notify, void *data)static bool vfio_intx_enable (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev =PCI_DEVICE (vdev); -uint8_t pin =vfio_pci_read_config (pdev,PCI_INTERRUPT_PIN ,1 ); +uint32_t val =vfio_pci_read_config (pdev,PCI_INTERRUPT_PIN ,1 ); +uint8_t pin;Error *err =NULL ;int32_t fd; +if (val == (uint32_t )-1 ) { +error_setg (errp,"failed to read PCI_INTERRUPT_PIN" ); +return false; + } + pin = val;if (!pin) {return true;
@@ -2764,6 +2770,7 @@ bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp)void vfio_pci_pre_reset (VFIOPCIDevice *vdev) {PCIDevice *pdev =PCI_DEVICE (vdev); +uint32_t val;uint16_t cmd;vfio_disable_interrupts (vdev);
@@ -2772,23 +2779,34 @@ void vfio_pci_pre_reset(VFIOPCIDevice *vdev)* Stop any ongoing DMA by disconnecting I/O, MMIO, and bus master. * Also put INTx Disable in known state. */ - cmd =vfio_pci_read_config (pdev,PCI_COMMAND ,2 ); - cmd &= ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY |PCI_COMMAND_MASTER | -PCI_COMMAND_INTX_DISABLE ); -vfio_pci_write_config (pdev,PCI_COMMAND , cmd,2 ); + val =vfio_pci_read_config (pdev,PCI_COMMAND ,2 ); +if (val != (uint32_t )-1 ) { + cmd = val; + cmd &= ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY |PCI_COMMAND_MASTER | +PCI_COMMAND_INTX_DISABLE ); +vfio_pci_write_config (pdev,PCI_COMMAND , cmd,2 ); + }/* Make sure the device is in D0 */ if (pdev->pm_cap) {uint16_t pmcsr;uint8_t state; - pmcsr =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); + val =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); +if (val == (uint32_t )-1 ) { +return ; + } + pmcsr = val; state = pmcsr &PCI_PM_CTRL_STATE_MASK ;if (state) { pmcsr &= ~PCI_PM_CTRL_STATE_MASK ;vfio_pci_write_config (pdev, pdev->pm_cap +PCI_PM_CTRL , pmcsr,2 );/* vfio handles the necessary delay here */ - pmcsr =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); + val =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); +if (val == (uint32_t )-1 ) { +return ; + } + pmcsr = val; state = pmcsr &PCI_PM_CTRL_STATE_MASK ;if (state) { error_report("vfio: Unable to power on device, stuck in D%d",
Signature · 1 line
2.53.0
Re: [PATCH v2 1/2] vfio/pci: don't narrow a failed config read to a plausible value
Quoted 8 lines · Denis V. Lunev
vfio_pci_read_config()signals a failed host-side read by returning (uint32_t)-1, regardless of the requested length.vfio_intx_enable()andvfio_pci_pre_reset()both narrowed that return value straight into a uint8_t/uint16_t local before checking anything, which truncates -1 into0xffor0xffff- values a real 1- or 2-byte register read can legitimately produce. From that point on, a failed read and real all-ones content are indistinguishable.
Quoted 8 lines
Keep the full
uint32_tresult and check it against (uint32_t)-1 before narrowing. Invfio_pci_pre_reset(), skip the corresponding write-back on a failed read instead of writing back constructed garbage to the device.Signed-off-by Denis V. Lunev <[email protected]>CC Alex Williamson <[email protected]>CC "Cédric Le Goater" <[email protected]>
Coverity CID: 1663688, 1663684
Thanks,
C.
Quoted 76 lines
---
hw/vfio/pci.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-)hw/vfio/pci.c +25
-7@@ -323,10 +323,16 @@ static void vfio_irqchip_change(Notifier *notify, void *data)static bool vfio_intx_enable (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev =PCI_DEVICE (vdev); -uint8_t pin =vfio_pci_read_config (pdev,PCI_INTERRUPT_PIN ,1 ); +uint32_t val =vfio_pci_read_config (pdev,PCI_INTERRUPT_PIN ,1 ); +uint8_t pin;Error *err =NULL ;int32_t fd; +if (val == (uint32_t )-1 ) { +error_setg (errp,"failed to read PCI_INTERRUPT_PIN" ); +return false; + } + pin = val;if (!pin) {return true;@@ -2764,6 +2770,7 @@ bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp)void vfio_pci_pre_reset (VFIOPCIDevice *vdev) {PCIDevice *pdev =PCI_DEVICE (vdev); +uint32_t val;uint16_t cmd;vfio_disable_interrupts (vdev);@@ -2772,23 +2779,34 @@ void vfio_pci_pre_reset(VFIOPCIDevice *vdev)* Stop any ongoing DMA by disconnecting I/O, MMIO, and bus master. * Also put INTx Disable in known state. */ - cmd =vfio_pci_read_config (pdev,PCI_COMMAND ,2 ); - cmd &= ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY |PCI_COMMAND_MASTER | -PCI_COMMAND_INTX_DISABLE ); -vfio_pci_write_config (pdev,PCI_COMMAND , cmd,2 ); + val =vfio_pci_read_config (pdev,PCI_COMMAND ,2 ); +if (val != (uint32_t )-1 ) { + cmd = val; + cmd &= ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY |PCI_COMMAND_MASTER | +PCI_COMMAND_INTX_DISABLE ); +vfio_pci_write_config (pdev,PCI_COMMAND , cmd,2 ); + }/* Make sure the device is in D0 */ if (pdev->pm_cap) {uint16_t pmcsr;uint8_t state; - pmcsr =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); + val =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); +if (val == (uint32_t )-1 ) { +return ; + } + pmcsr = val; state = pmcsr &PCI_PM_CTRL_STATE_MASK ;if (state) { pmcsr &= ~PCI_PM_CTRL_STATE_MASK ;vfio_pci_write_config (pdev, pdev->pm_cap +PCI_PM_CTRL , pmcsr,2 );/* vfio handles the necessary delay here */ - pmcsr =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); + val =vfio_pci_read_config (pdev, pdev->pm_cap +PCI_PM_CTRL ,2 ); +if (val == (uint32_t )-1 ) { +return ; + } + pmcsr = val; state = pmcsr &PCI_PM_CTRL_STATE_MASK ;if (state) { error_report("vfio: Unable to power on device, stuck in D%d",
Re: [PATCH v2 1/2] vfio/pci: don't narrow a failed config read to a plausible value
In reply to Denis V. LunevQuoted 8 lines · Denis V. Lunev
vfio_pci_read_config()signals a failed host-side read by returning (uint32_t)-1, regardless of the requested length.vfio_intx_enable()andvfio_pci_pre_reset()both narrowed that return value straight into a uint8_t/uint16_t local before checking anything, which truncates -1 into0xffor0xffff- values a real 1- or 2-byte register read can legitimately produce. From that point on, a failed read and real all-ones content are indistinguishable.
Quoted 8 lines
Keep the full
uint32_tresult and check it against (uint32_t)-1 before narrowing. Invfio_pci_pre_reset(), skip the corresponding write-back on a failed read instead of writing back constructed garbage to the device.Signed-off-by Denis V. Lunev <[email protected]>CC Alex Williamson <[email protected]>CC "Cédric Le Goater" <[email protected]>
This feels like qemu-stable material too. I'm picking it up, please let me know if I should not.
Thanks,
/mjt
PATCH v2 2/2 vfio/pci: reject invalid PCI_INTERRUPT_PIN values
In reply to Denis V. Lunevqemu-kvm aborts a few seconds after starting a VM with a passed-through GPU whose PCI_INTERRUPT_PIN comes back as an out-of-range value: vfio_intx_enable() only guards against pin == 0 and stores vdev->intx.pin = pin - 1 with no upper-bound check. That value later reaches pci_irq_handler()'sassert(0 <= irq_num && irq_num < PCI_NUM_PINS) via pci_irq_deassert() -> pci_set_irq(), aborting the process.
Legal PCI_INTERRUPT_PIN values are 0 (no legacy interrupt) or 1-PCI_NUM_PINS (INTA-INTD); reject anything else before it reaches vdev->intx.pin, whether the out-of-range value came from a read failure (now caught by the previous commit) or was handed back as data by the device itself.
---
hw/vfio/pci.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/vfio/pci.c +5 -0
@@ -338,6 +338,11 @@ static bool vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)return true; } +if (pin >PCI_NUM_PINS ) { +error_setg (errp,"invalid PCI interrupt pin %d" , pin); +return false; + } +/* * Do not alter interrupt state during vfio_realize and cpr load. * The incoming state is cleared thereafter.
Signature · 1 line
2.53.0
Re: [PATCH v2 2/2] vfio/pci: reject invalid PCI_INTERRUPT_PIN values
Quoted 14 lines · Denis V. Lunev
qemu-kvm aborts a few seconds after starting a VM with a passed-through GPU whose
PCI_INTERRUPT_PINcomes back as an out-of-range value:vfio_intx_enable()only guards against pin == 0 and storesvdev->intx.pin= pin - 1 with no upper-bound check. That value later reaches pci_irq_handler()'sassert(0 <= irq_num && irq_num < PCI_NUM_PINS)viapci_irq_deassert()->pci_set_irq(), aborting the process.Legal
PCI_INTERRUPT_PINvalues are 0 (no legacy interrupt) or 1-PCI_NUM_PINS (INTA-INTD); reject anything else before it reachesvdev->intx.pin, whether the out-of-range value came from a read failure (now caught by the previous commit) or was handed back as data by the device itself.
Quoted 4 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Alex Williamson <[email protected]>CC "Cédric Le Goater" <[email protected]>---
Thanks,
C.
Quoted 19 lines
hw/vfio/pci.c | 5 +++++ 1 file changed, 5 insertions(+)hw/vfio/pci.c +5
-0@@ -338,6 +338,11 @@ static bool vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)return true; } +if (pin >PCI_NUM_PINS ) { +error_setg (errp,"invalid PCI interrupt pin %d" , pin); +return false; + } +/* * Do not alter interrupt state during vfio_realize and cpr load. * The incoming state is cleared thereafter.
Re: [PATCH v2 2/2] vfio/pci: reject invalid PCI_INTERRUPT_PIN values
In reply to Denis V. LunevQuoted 14 lines · Denis V. Lunev
qemu-kvm aborts a few seconds after starting a VM with a passed-through GPU whose
PCI_INTERRUPT_PINcomes back as an out-of-range value:vfio_intx_enable()only guards against pin == 0 and storesvdev->intx.pin= pin - 1 with no upper-bound check. That value later reaches pci_irq_handler()'sassert(0 <= irq_num && irq_num < PCI_NUM_PINS)viapci_irq_deassert()->pci_set_irq(), aborting the process.Legal
PCI_INTERRUPT_PINvalues are 0 (no legacy interrupt) or 1-PCI_NUM_PINS (INTA-INTD); reject anything else before it reachesvdev->intx.pin, whether the out-of-range value came from a read failure (now caught by the previous commit) or was handed back as data by the device itself.
Quoted 7 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Alex Williamson <[email protected]>CC "Cédric Le Goater" <[email protected]>---
hw/vfio/pci.c | 5 +++++ 1 file changed, 5 insertions(+)
Re: [PATCH v2 2/2] vfio/pci: reject invalid PCI_INTERRUPT_PIN values
In reply to Denis V. LunevQuoted 14 lines · Denis V. Lunev
qemu-kvm aborts a few seconds after starting a VM with a passed-through GPU whose
PCI_INTERRUPT_PINcomes back as an out-of-range value:vfio_intx_enable()only guards against pin == 0 and storesvdev->intx.pin= pin - 1 with no upper-bound check. That value later reaches pci_irq_handler()'sassert(0 <= irq_num && irq_num < PCI_NUM_PINS)viapci_irq_deassert()->pci_set_irq(), aborting the process.Legal
PCI_INTERRUPT_PINvalues are 0 (no legacy interrupt) or 1-PCI_NUM_PINS (INTA-INTD); reject anything else before it reachesvdev->intx.pin, whether the out-of-range value came from a read failure (now caught by the previous commit) or was handed back as data by the device itself.
Quoted 3 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Alex Williamson <[email protected]>CC "Cédric Le Goater" <[email protected]>
And this one too, feels like it should go to qemu-stable. I'm picking it up (with context fixup for 10.0.x), please let me know if I should not.
Thanks,
/mjt