Skip to contentSkip to conversation

of: handle NULL node in of_get_next_available_child

PATCH In review Florian Fainelli 2 participants 4 messages May 23, 2014 – May 27, 2014 Device tree

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of: handle NULL node in of_get_next_available_child

Add an early check for the node argument in of_get_next_available_child() to avoid dereferencing a NULL node pointer a few lines after.

---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/of/base.c +3 -0
index e67b308819c9..9a71bfac78ae 100644
@@ -746,6 +746,9 @@ struct device_node *of_get_next_available_child(const struct device_node *node,
 	struct device_node *next;
 	unsigned long flags;
 
+	if (!node)
+		return NULL;
+
 	raw_spin_lock_irqsave(&devtree_lock, flags);
 	next = prev ? prev->sibling : node->child;
 	for (; next; next = next->sibling) {
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw
Quoted 6 lines · Florian Fainelli

Add an early check for the node argument in of_get_next_available_child() to avoid dereferencing a NULL node pointer a few lines after.

Is there a bug that exposed this path? I'd like some more justification before merging this patch, particularly considering of_get_next_child() doesn't check for it either, but you're not modifying that function.

g.

Quoted 21 lines
---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/of/base.c +3 -0
index e67b308819c9..9a71bfac78ae 100644
@@ -746,6 +746,9 @@ struct device_node *of_get_next_available_child(const struct device_node *node,
 	struct device_node *next;
 	unsigned long flags;
 
+	if (!node)
+		return NULL;
+
 	raw_spin_lock_irqsave(&devtree_lock, flags);
 	next = prev ? prev->sibling : node->child;
 	for (; next; next = next->sibling) {
Signature · 1 line
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw

Hi Grant,

2014-05-27 4:19 GMT-07:00 Grant Likely <[email protected]>:

Quoted 9 lines · Florian Fainelli
Quoted 6 lines

Add an early check for the node argument in of_get_next_available_child() to avoid dereferencing a NULL node pointer a few lines after.

Is there a bug that exposed this path?

Daniel sent a patch recently [1] which makes us call for_each_available_child_of_node() on a potentially NULL node argument.

The reason for that is that the loop is supposed to iterate over child nodes (Ethernet PHY nodes) of a given MDIO bus node. There might be non-DT probed MDIO buses registered in the system (e.g: the fixed MDIO bus), which do not have a valid of_node pointer. Since then, Daniel updated his patch to also check for the MDIO bus device_node pointer to be non-NULL, but I feel like we should also but defensive here. Presumably this situation could happen with e.g: i2c busses, spi busses in a system that mixes both DT-probed and non-DT probed devices.

Quoted 3 lines

I'd like some more justification
before merging this patch, particularly considering of_get_next_child() doesn't check for it either, but you're not modifying that function.

Good catch, it should also be updated.

[1]: http://patchwork.ozlabs.org/patch/347712/

Quoted 25 lines

g.

Quoted 21 lines
---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/of/base.c +3 -1
index e67b308819c9..9a71bfac78ae 100644
@@ -746,6 +746,9 @@ struct device_node *of_get_next_available_child(const struct device_node *node,
      struct device_node *next;
      unsigned long flags;
 
+     if (!node)
+             return NULL;
+
      raw_spin_lock_irqsave(&devtree_lock, flags);
      next = prev ? prev->sibling : node->child;
      for (; next; next = next->sibling) {
--

1.9.1

Signature · 5 lines
Florian
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
Quoted 8 lines · Florian Fainelli

Hi Grant,

2014-05-27 4:19 GMT-07:00 Grant Likely <[email protected]>:

Quoted 5 lines · Florian Fainelli
Quoted 4 lines

Add an early check for the node argument in of_get_next_available_child() to avoid dereferencing a NULL node pointer a few lines after.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Is there a bug that exposed this path?

Daniel sent a patch recently [1] which makes us call for_each_available_child_of_node() on a potentially NULL node argument.

And yet in your reply to that patch you argued that the NULL check should be in the caller!

Anyway, this isn't a big deal. Post a patch fixing both and I'll apply it.

g.
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html