From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Denis V. Lunev" <den@openvz.org>,
"Alex Williamson" <alex@shazbot.org>,
"Cédric Le Goater" <clg@redhat.com>
Subject: [PULL 03/13] vfio/pci: don't narrow a failed config read to a plausible value
Date: Tue, 21 Jul 2026 19:05:08 +0200 [thread overview]
Message-ID: <20260721170518.4160785-4-clg@redhat.com> (raw)
In-Reply-To: <20260721170518.4160785-1-clg@redhat.com>
From: "Denis V. Lunev" <den@openvz.org>
vfio_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.
Resolves: Coverity CID 1663684
Resolves: Coverity CID 1663688
Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Alex Williamson <alex@shazbot.org>
CC: Cédric Le Goater <clg@redhat.com>
Link: https://lore.kernel.org/qemu-devel/20260717122232.468955-2-den@openvz.org
[ clg: Added Coverity IDs ]
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
hw/vfio/pci.c | 32 +++++++++++++++++++++++++-------
1 file changed, 25 insertions(+), 7 deletions(-)
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index dcfc92aae1c78d3a803665bcaea1e83f66a6d030..b8c937d4be50091691875f2253c4d03ba83df704 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -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;
@@ -2766,6 +2772,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);
@@ -2774,23 +2781,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",
--
2.55.0
next prev parent reply other threads:[~2026-07-21 17:06 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-21 17:05 [PULL 00/13] vfio queue Cédric Le Goater
2026-07-21 17:05 ` [PULL 01/13] vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load time Cédric Le Goater
2026-07-21 17:05 ` [PULL 02/13] vfio/region: Clarify dma-buf failure messages Cédric Le Goater
2026-07-21 17:05 ` Cédric Le Goater [this message]
2026-07-21 17:05 ` [PULL 04/13] vfio/pci: reject invalid PCI_INTERRUPT_PIN values Cédric Le Goater
2026-07-21 17:05 ` [PULL 05/13] vfio-user: vfio_user_get_region_info: prevent buffer overflow Cédric Le Goater
2026-07-21 17:05 ` [PULL 06/13] vfio-user: vfio_user_get_region_info: respect max_xfer_size Cédric Le Goater
2026-07-21 17:05 ` [PULL 07/13] vfio-user: vfio_user_get_region_info: reject unreasonably short struct Cédric Le Goater
2026-07-21 17:05 ` [PULL 08/13] vfio-user: vfio_user_get_region_info: prevent excessive malloc Cédric Le Goater
2026-07-21 17:05 ` [PULL 09/13] vfio-user: vfio_user_device_io_get_region_info: fix capability check Cédric Le Goater
2026-07-21 17:05 ` [PULL 10/13] vfio-user: vfio_user_device_io_device_feature: prevent buffer overflow Cédric Le Goater
2026-07-21 17:05 ` [PULL 11/13] vfio-user: vfio_user_device_io_device_feature: prevent excessive malloc Cédric Le Goater
2026-07-21 17:05 ` [PULL 12/13] vfio-user: vfio_user_device_io_set_irqs: prevent buffer overflow Cédric Le Goater
2026-07-21 17:05 ` [PULL 13/13] vfio-user: vfio_user_device_io_set_irqs: prevent excessive malloc Cédric Le Goater
2026-07-22 17:58 ` [PULL 00/13] vfio queue Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260721170518.4160785-4-clg@redhat.com \
--to=clg@redhat.com \
--cc=alex@shazbot.org \
--cc=den@openvz.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.