linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@redhat.com>
To: linux-pci@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
	"Myron Stowe" <myron.stowe@redhat.com>,
	"Joe Lawrence" <Joe.Lawrence@stratus.com>,
	"Kenji Kaneshige" <kaneshige.kenji@jp.fujitsu.com>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>
Subject: [PATCH] PCI: avoid NULL deref in alloc_pcie_link_state
Date: Wed, 19 Jun 2013 20:56:14 +0200	[thread overview]
Message-ID: <1371668174-32115-1-git-send-email-rkrcmar@redhat.com> (raw)

PCIe switch upstream port can be connected directly to the PCIe root bus
in QEMU; ASPM does not expect this topology and dereferences NULL pointer
when initializing.

I have not confirmed this can happen on real hardware, but it is presented
as a feature in QEMU, so there is no reason to panic if we can recover.

The dereference happens with topology defined by
  -M q35 -device x3130-upstream,bus=pcie.0,id=upstream \
  -device xio3130-downstream,bus=upstream,id=downstream,chassis=1
where on line drivers/pci/pcie/aspm.c:530 (alloc_pcie_link_state+13):
  		parent = pdev->bus->parent->self->link_state;
"pdev->bus->parent->self == NULL", because "pdev->bus->parent" has no
"->parent", hence no "->self".

Even though discouraged by QEMU documentation, one can set up even
topology without the upstream port
  -M q35 -device xio3130-downstream,bus=pcie.0,id=downstream,chassis=1
so "pdev->bus->parent == NULL", because "pdev->bus" is the root bus.
The patch checks for this too, because I do not like *NULL.

Right now, PCIe switch has to connect to the root port
  -M q35 -device ioh3420,bus=pcie.0,id=root.0 \
  -device x3130-upstream,bus=root.0,id=upstream \
  -device xio3130-downstream,bus=upstream,id=downstream,chassis=1

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
 drivers/pci/pcie/aspm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 403a443..1ad1514 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -527,8 +527,8 @@ static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
 	link->pdev = pdev;
 	if (pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM) {
 		struct pcie_link_state *parent;
-		parent = pdev->bus->parent->self->link_state;
-		if (!parent) {
+		if (!pdev->bus->parent || !pdev->bus->parent->self ||
+		    !(parent = pdev->bus->parent->self->link_state)) {
 			kfree(link);
 			return NULL;
 		}
-- 
1.8.1.4


             reply	other threads:[~2013-06-19 18:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-19 18:56 Radim Krčmář [this message]
2013-06-25  1:38 ` [PATCH] PCI: avoid NULL deref in alloc_pcie_link_state Bjorn Helgaas
2013-06-25  2:58   ` Alex Williamson
2013-06-25  3:35     ` Bjorn Helgaas
2013-06-25  3:57       ` Alex Williamson
2013-06-25 11:23   ` Michael S. Tsirkin
2013-06-25 17:17     ` Bjorn Helgaas
2013-06-25 20:50       ` Radim Krčmář
  -- strict thread matches above, loose matches on Subject: below --
2013-08-08 13:57 Radim Krčmář
2013-08-23  0:02 ` Bjorn Helgaas
2013-08-23 21:46   ` Bjorn Helgaas

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=1371668174-32115-1-git-send-email-rkrcmar@redhat.com \
    --to=rkrcmar@redhat.com \
    --cc=Joe.Lawrence@stratus.com \
    --cc=bhelgaas@google.com \
    --cc=kaneshige.kenji@jp.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=myron.stowe@redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).