Skip to contentSkip to conversation

media: cobalt: fix null-ptr-deref when there is no PCI bridge

PATCH In review Tong Zhang 2 participants 4 messages Apr 24, 2021 – May 26, 2021

Subsystems (2)
lore
Share & tools
lore

Tong Zhang

author
#0 lore raw

PATCH 1/1 media: cobalt: fix null-ptr-deref when there is no PCI bridge

the PCI bridge might be NULL, so we'd better check before use it

[    1.870569] RIP: 0010:pcie_bus_link_get_lanes.isra.0+0x26/0x59 [cobalt]
[    1.875880] Call Trace:
[    1.876013]  cobalt_probe.cold+0x1be/0xc11 [cobalt]
[    1.876683]  pci_device_probe+0x10f/0x1c0
---
 drivers/media/pci/cobalt/cobalt-driver.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/media/pci/cobalt/cobalt-driver.c +4 -0
index 0695078ef812..5687ed4869ac 100644
@@ -189,6 +189,8 @@ void cobalt_pcie_status_show(struct cobalt *cobalt)
 	u32 capa;
 	u16 stat, ctrl;
 
+	if (!pci_bus_dev)
+		return;
 	if (!pci_is_pcie(pci_dev) || !pci_is_pcie(pci_bus_dev))
 		return;
 
@@ -247,6 +249,8 @@ static unsigned pcie_bus_link_get_lanes(struct cobalt *cobalt)
 	struct pci_dev *pci_dev = cobalt->pci_dev->bus->self;
 	u32 link;
 
+	if (!pci_dev)
+		return 0;
 	if (!pci_is_pcie(pci_dev))
 		return 0;
 	pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &link);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

[    1.870569] RIP: 0010:pcie_bus_link_get_lanes.isra.0+0x26/0x59 [cobalt]
[    1.875880] Call Trace:
[    1.876013]  cobalt_probe.cold+0x1be/0xc11 [cobalt]
[    1.876683]  pci_device_probe+0x10f/0x1c0

How did you test this? With some virtualized PCI bus or something? I'm not sure how this can happen.

Regards,

Hans

Quoted 29 lines
---
 drivers/media/pci/cobalt/cobalt-driver.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/media/pci/cobalt/cobalt-driver.c +4 -0
index 0695078ef812..5687ed4869ac 100644
@@ -189,6 +189,8 @@ void cobalt_pcie_status_show(struct cobalt *cobalt)
 	u32 capa;
 	u16 stat, ctrl;
 
+	if (!pci_bus_dev)
+		return;
 	if (!pci_is_pcie(pci_dev) || !pci_is_pcie(pci_bus_dev))
 		return;
 
@@ -247,6 +249,8 @@ static unsigned pcie_bus_link_get_lanes(struct cobalt *cobalt)
 	struct pci_dev *pci_dev = cobalt->pci_dev->bus->self;
 	u32 link;
 
+	if (!pci_dev)
+		return 0;
 	if (!pci_is_pcie(pci_dev))
 		return 0;
 	pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &link);
author
#2 lore raw
Quoted 4 lines · Hans Verkuil
Quoted 2 lines · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

Quoted 8 lines
Quoted 4 lines
[    1.870569] RIP: 0010:pcie_bus_link_get_lanes.isra.0+0x26/0x59 [cobalt]
[    1.875880] Call Trace:
[    1.876013]  cobalt_probe.cold+0x1be/0xc11 [cobalt]
[    1.876683]  pci_device_probe+0x10f/0x1c0

How did you test this? With some virtualized PCI bus or something? I'm not sure how this can happen.

Hello Hans,
IMHO bus->self is pci bridge device and we can have a system configuration that does not have such a bridge,
in this case, bus->self is NULL.
- Tong

Quoted 34 lines

Regards,

Hans

Quoted 29 lines
---
 drivers/media/pci/cobalt/cobalt-driver.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/media/pci/cobalt/cobalt-driver.c +4 -0
index 0695078ef812..5687ed4869ac 100644
@@ -189,6 +189,8 @@ void cobalt_pcie_status_show(struct cobalt *cobalt)
      u32 capa;
      u16 stat, ctrl;
 
+     if (!pci_bus_dev)
+             return;
      if (!pci_is_pcie(pci_dev) || !pci_is_pcie(pci_bus_dev))
              return;
 
@@ -247,6 +249,8 @@ static unsigned pcie_bus_link_get_lanes(struct cobalt *cobalt)
      struct pci_dev *pci_dev = cobalt->pci_dev->bus->self;
      u32 link;
 
+     if (!pci_dev)
+             return 0;
      if (!pci_is_pcie(pci_dev))
              return 0;
      pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &link);
#3 lore raw
Quoted 10 lines · Tong Zhang
Quoted 9 lines · Hans Verkuil
Quoted 6 lines · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

[    1.870569] RIP: 0010:pcie_bus_link_get_lanes.isra.0+0x26/0x59 [cobalt]
[    1.875880] Call Trace:
[    1.876013]  cobalt_probe.cold+0x1be/0xc11 [cobalt]
[    1.876683]  pci_device_probe+0x10f/0x1c0
Quoted 7 lines
Quoted 3 lines

How did you test this? With some virtualized PCI bus or something? I'm not sure how this can happen.

Hello Hans,
IMHO bus->self is pci bridge device and we can have a system configuration that does not have such a bridge,
in this case, bus->self is NULL.

That does not answer my question :-)

You show a backtrace in the commit message, how did you get that?

Regards,

Hans

Quoted 35 lines

- Tong

Quoted 34 lines

Regards,

Hans

Quoted 29 lines
---
 drivers/media/pci/cobalt/cobalt-driver.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/media/pci/cobalt/cobalt-driver.c +4 -0
index 0695078ef812..5687ed4869ac 100644
@@ -189,6 +189,8 @@ void cobalt_pcie_status_show(struct cobalt *cobalt)
      u32 capa;
      u16 stat, ctrl;
 
+     if (!pci_bus_dev)
+             return;
      if (!pci_is_pcie(pci_dev) || !pci_is_pcie(pci_bus_dev))
              return;
 
@@ -247,6 +249,8 @@ static unsigned pcie_bus_link_get_lanes(struct cobalt *cobalt)
      struct pci_dev *pci_dev = cobalt->pci_dev->bus->self;
      u32 link;
 
+     if (!pci_dev)
+             return 0;
      if (!pci_is_pcie(pci_dev))
              return 0;
      pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &link);