Skip to contentSkip to conversation

PCI/hotplug: fix potential null pointer deference

PATCH In review Young Xiao 3 participants 3 messages Jun 14, 2019 – Aug 2, 2019 PCI

lore
Share & tools
lore

Young Xiao

author
#0 lore raw

PATCH 1/1 PCI/hotplug: fix potential null pointer deference

There is otherwise a risk of a null pointer dereference.

---
 drivers/pci/hotplug/cpqphp_ctrl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/pci/hotplug/cpqphp_ctrl.c +6 -4
index b7f4e1f..3c8399f 100644
@@ -598,10 +598,11 @@ static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size
 			*head = node->next;
 		} else {
 			prevnode = *head;
-			while (prevnode->next != node)
+			while (prevnode && prevnode->next != node)
 				prevnode = prevnode->next;
 
-			prevnode->next = node->next;
+			if (prevnode)
+				prevnode->next = node->next;
 		}
 		node->next = NULL;
 		break;
@@ -788,10 +789,11 @@ static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
 			*head = node->next;
 		} else {
 			prevnode = *head;
-			while (prevnode->next != node)
+			while (prevnode && prevnode->next != node)
 				prevnode = prevnode->next;
 
-			prevnode->next = node->next;
+			if (prevnode)
+				prevnode->next = node->next;
 		}
 		node->next = NULL;
 		break;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 2 lines · Young Xiao

There is otherwise a risk of a null pointer dereference.

Had you analyze the code?
How come that prevnode can become NULL?

Quoted 40 lines
---
 drivers/pci/hotplug/cpqphp_ctrl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/pci/hotplug/cpqphp_ctrl.c +6 -5
index b7f4e1f..3c8399f 100644
@@ -598,10 +598,11 @@ static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size
                        *head = node->next;
                } else {
                        prevnode = *head;
-                       while (prevnode->next != node)
+                       while (prevnode && prevnode->next != node)
                                prevnode = prevnode->next;
 
-                       prevnode->next = node->next;
+                       if (prevnode)
+                               prevnode->next = node->next;
                }
                node->next = NULL;
                break;
@@ -788,10 +789,11 @@ static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
                        *head = node->next;
                } else {
                        prevnode = *head;
-                       while (prevnode->next != node)
+                       while (prevnode && prevnode->next != node)
                                prevnode = prevnode->next;
 
-                       prevnode->next = node->next;
+                       if (prevnode)
+                               prevnode->next = node->next;
                }
                node->next = NULL;
                break;
--

2.7.4

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 2 lines · Young Xiao

There is otherwise a risk of a null pointer dereference.

Had you analyze the code?
How come that prevnode can become NULL?

Dropping this for now. Young, if there's a scenario where prevnode can actually be NULL, please mention that in the changelog and repost this.

Quoted 45 lines
Quoted 40 lines
---
 drivers/pci/hotplug/cpqphp_ctrl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/pci/hotplug/cpqphp_ctrl.c +6 -5
index b7f4e1f..3c8399f 100644
@@ -598,10 +598,11 @@ static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size
                        *head = node->next;
                } else {
                        prevnode = *head;
-                       while (prevnode->next != node)
+                       while (prevnode && prevnode->next != node)
                                prevnode = prevnode->next;
 
-                       prevnode->next = node->next;
+                       if (prevnode)
+                               prevnode->next = node->next;
                }
                node->next = NULL;
                break;
@@ -788,10 +789,11 @@ static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
                        *head = node->next;
                } else {
                        prevnode = *head;
-                       while (prevnode->next != node)
+                       while (prevnode && prevnode->next != node)
                                prevnode = prevnode->next;
 
-                       prevnode->next = node->next;
+                       if (prevnode)
+                               prevnode->next = node->next;
                }
                node->next = NULL;
                break;
--

2.7.4

Signature · 2 lines
With Best Regards,
Andy Shevchenko