device property: fix child iteration issues with secondary fwnodes
Subsystems (4)
Andy Shevchenko
reviewerv4 cover device property: fix child iteration issues with secondary fwnodes
This series fixes two issues in the fwnode child iteration logic when a secondary fwnode is present.
The first issue is a refcount imbalance in software_node_get_next_child(). When a software node is used as a secondary fwnode, the iteration code may incorrectly decrement the refcount of child nodes that do not belong to the software node hierarchy. This results in refcount underflow and possible use-after-free.
The second issue is an infinite loop in fwnode_for_each_child_node(), caused by improper handling of iteration state across primary and secondary fwnodes. When iterating over children from both primary and secondary fwnodes, the code may incorrectly resume iteration from the primary fwnode even when the current child belongs to the secondary, leading to repeated traversal and a loop.
Both issues are triggered when mixing different fwnode types through the secondary mechanism, and stem from incorrect assumptions about ownership and traversal context of child nodes.
Changes in v4:
- amended the fix and test case (Andy)
- added patch 2 to align other implementations with RAII approach (Andy)
- tested on Intel Galileo board for which the initial code was developed (Andy)
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- remove software node patch
- add a kunit test case suggested by Andy Shevchenko
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- use __free() to cleanup parent fwnode
- Link to v1: https://lore.kernel.org/r/[email protected]
Andy Shevchenko (1):
device property: Refactor to use RAII approach
Xu Yang (2):
device property: fix infinite loop in fwnode_for_each_child_node() device property: add test cases for fwnode_for_each_child_node()
drivers/base/property.c | 41 ++++--- drivers/base/test/Kconfig | 1 + drivers/base/test/property-entry-test.c | 136 ++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 17 deletions(-)
Signature · 1 line
2.50.1
Andy Shevchenko
reviewerPATCH v4 1/3 device property: fix infinite loop in fwnode_for_each_child_node()
From: Xu Yang <[email protected]>
When iterate over children of a fwnode that has a secondary fwnode, fwnode_get_next_child_node() can enter an infinite loop if the secondary fwnode has more than one child.
Parent Child
(Primary fwnode) FWa: {FWa1, FWa2, FWa3}
(Secondary fwnode) FWb: {FWb1, FWb2}In this case:
┌─> fwnode_get_next_child_node(FWa, FWa1) │ - fwnode_call_ptr_op(FWa, get_next_child_node, FWa1) returns FWa2 │ │ ... │ │ fwnode_get_next_child_node(FWa, FWa3) │ - fwnode_call_ptr_op(FWa, get_next_child_node, FWa3) returns NULL │ - fwnode_call_ptr_op(FWb, get_next_child_node, FWa3) returns FWb1 │ │ fwnode_get_next_child_node(FWa, FWb1) │ - fwnode_call_ptr_op(FWa, get_next_child_node, FWb1) returns FWa1 └────┘
This cause fwnode_for_each_child_node() to loop indefinitely, reapeatedly output {FWa1, FWa2, FWa3, FWb1, FWa1, ...}.
The root cause is that when the current child (FWb1) belongs to the secondary fwnode, calling get_next_child_node() on the parimary fwnode incorrectly returns the first child (FWa1) again instead of NULL.
Fix this by dynamically checking the parent fwnode of the current child before calling get_next_child_node(). This approach follows the pattern established in commit b5b41ab6b0c1 ("device property: Check fwnode->secondary in fwnode_graph_get_next_endpoint()").
--- drivers/base/property.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/base/property.c +16 -3
@@ -807,18 +807,31 @@ struct fwnode_handle * fwnode_get_next_child_node(const struct fwnode_handle *fwnode, struct fwnode_handle *child) { + const struct fwnode_handle *parent; + struct fwnode_handle *child_parent __free(fwnode_handle) = NULL;struct fwnode_handle *next; -if (IS_ERR_OR_NULL (fwnode)) +/* +* If this function is in a loop and the previous iteration returned +* an child from fwnode->secondary, then we need to use the secondary +* as parent rather than @fwnode. +*/ +if (child) { + child_parent =fwnode_get_parent (child); + parent = child_parent; + }else { + parent = fwnode; + } +if (IS_ERR_OR_NULL (parent))return NULL ;/* Try to find a child in primary fwnode */ - next =fwnode_call_ptr_op (fwnode, get_next_child_node, child); + next =fwnode_call_ptr_op (parent, get_next_child_node, child);if (next)return next;/* When no more children in primary, continue with secondary */ -return fwnode_call_ptr_op (fwnode->secondary, get_next_child_node, child); +return fwnode_get_next_child_node (parent->secondary,NULL ); }EXPORT_SYMBOL_GPL (fwnode_get_next_child_node);
Signature · 1 line
2.50.1
Re: [PATCH v4 1/3] device property: fix infinite loop in fwnode_for_each_child_node()
Quoted 37 lines · Andy Shevchenko
From: Xu Yang <[email protected]>
When iterate over children of a fwnode that has a secondary fwnode,
fwnode_get_next_child_node()can enter an infinite loop if the secondary fwnode has more than one child.Parent Child (Primary fwnode) FWa: {FWa1, FWa2, FWa3} (Secondary fwnode) FWb: {FWb1, FWb2}In this case:
┌─> fwnode_get_next_child_node(FWa, FWa1) │ - fwnode_call_ptr_op(FWa, get_next_child_node, FWa1) returns FWa2 │ │ ... │ │ fwnode_get_next_child_node(FWa, FWa3) │ - fwnode_call_ptr_op(FWa, get_next_child_node, FWa3) returns NULL │ - fwnode_call_ptr_op(FWb, get_next_child_node, FWa3) returns FWb1 │ │ fwnode_get_next_child_node(FWa, FWb1) │ - fwnode_call_ptr_op(FWa, get_next_child_node, FWb1) returns FWa1 └────┘This cause
fwnode_for_each_child_node()to loop indefinitely, reapeatedly output {FWa1, FWa2, FWa3, FWb1, FWa1, ...}.The root cause is that when the current child (FWb1) belongs to the secondary fwnode, calling
get_next_child_node()on the parimary fwnode incorrectly returns the first child (FWa1) again instead of NULL.Fix this by dynamically checking the parent fwnode of the current child before calling
get_next_child_node(). This approach follows the pattern established in commitb5b41ab6b0c1("device property: Checkfwnode->secondaryinfwnode_graph_get_next_endpoint()").
Quoted 5 lines
Fixes 2692c614f8f0 ("device property: Allow secondary lookup in fwnode_get_next_child_node()")Signed-off-by Xu Yang <[email protected]>Tested-by Andy Shevchenko <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
Thanks,
Xu Yang
Quoted 46 lines
--- drivers/base/property.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)drivers/base/property.c +16
-3@@ -807,18 +807,31 @@ struct fwnode_handle * fwnode_get_next_child_node(const struct fwnode_handle *fwnode, struct fwnode_handle *child) { + const struct fwnode_handle *parent; + struct fwnode_handle *child_parent __free(fwnode_handle) = NULL;struct fwnode_handle *next; -if (IS_ERR_OR_NULL (fwnode)) +/* +* If this function is in a loop and the previous iteration returned +* an child from fwnode->secondary, then we need to use the secondary +* as parent rather than @fwnode. +*/ +if (child) { + child_parent =fwnode_get_parent (child); + parent = child_parent; + }else { + parent = fwnode; + } +if (IS_ERR_OR_NULL (parent))return NULL ;/* Try to find a child in primary fwnode */ - next =fwnode_call_ptr_op (fwnode, get_next_child_node, child); + next =fwnode_call_ptr_op (parent, get_next_child_node, child);if (next)return next;/* When no more children in primary, continue with secondary */ -return fwnode_call_ptr_op (fwnode->secondary, get_next_child_node, child); +return fwnode_get_next_child_node (parent->secondary,NULL ); }EXPORT_SYMBOL_GPL (fwnode_get_next_child_node);Signature · 1 line
2.50.1
Andy Shevchenko
reviewerPATCH v4 2/3 device property: Refactor to use RAII approach
In reply to Andy ShevchenkoIn a couple of functions code can be made cleaner with help of __free() macro. Refactor these to use RAII approach.
--- drivers/base/property.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/base/property.c +8 -14
@@ -7,10 +7,10 @@* Mika Westerberg <[email protected]> */ +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/err.h> #include <linux/export.h> -#include <linux/kconfig.h> #include <linux/of.h> #include <linux/property.h> #include <linux/phy.h>
@@ -517,7 +517,6 @@ EXPORT_SYMBOL_GPL(fwnode_property_read_string);int fwnode_property_match_string (const struct fwnode_handle *fwnode,const char *propname,const char *string) { -const char **values;int nval, ret; nval =fwnode_property_string_array_count (fwnode, propname);
@@ -527,20 +526,18 @@ int fwnode_property_match_string(const struct fwnode_handle *fwnode,if (nval ==0 )return -ENODATA ; - values =kcalloc (nval,sizeof (*values),GFP_KERNEL ); + const char **values __free(kfree) = kcalloc(nval, sizeof(*values), GFP_KERNEL);if (!values)return -ENOMEM ; ret =fwnode_property_read_string_array (fwnode, propname, values, nval);if (ret <0 ) - goto out_free; +return ret; ret =match_string (values, nval, string);if (ret <0 ) - ret = -ENODATA ; +return -ENODATA ; -out_free: -kfree (values);return ret; }EXPORT_SYMBOL_GPL (fwnode_property_match_string);
@@ -1128,8 +1125,9 @@ struct fwnode_handle * fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode, struct fwnode_handle *prev) { - struct fwnode_handle *ep, *port_parent = NULL; const struct fwnode_handle *parent; + struct fwnode_handle *port_parent __free(fwnode_handle) = NULL; +struct fwnode_handle *ep;/* * If this function is in a loop and the previous iteration returned
@@ -1147,13 +1145,9 @@ fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode, ep =fwnode_call_ptr_op (parent, graph_get_next_endpoint, prev);if (ep) - goto out_put_port_parent; +return ep; - ep =fwnode_graph_get_next_endpoint (parent->secondary,NULL ); - -out_put_port_parent: -fwnode_handle_put (port_parent); -return ep; +return fwnode_graph_get_next_endpoint (parent->secondary,NULL ); }EXPORT_SYMBOL_GPL (fwnode_graph_get_next_endpoint);
Signature · 1 line
2.50.1
Rafael J. Wysocki
maintainerRe: [PATCH v4 2/3] device property: Refactor to use RAII approach
Quoted 58 lines · Andy Shevchenko
In a couple of functions code can be made cleaner with help of
__free()macro. Refactor these to use RAII approach.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/base/property.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)drivers/base/property.c +4
-7@@ -7,10 +7,10 @@* Mika Westerberg <[email protected]> */ +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/err.h> #include <linux/export.h> -#include <linux/kconfig.h> #include <linux/of.h> #include <linux/property.h> #include <linux/phy.h> @@ -517,7 +517,6 @@ EXPORT_SYMBOL_GPL(fwnode_property_read_string);int fwnode_property_match_string (const struct fwnode_handle *fwnode,const char *propname,const char *string) { -const char **values;int nval, ret; nval =fwnode_property_string_array_count (fwnode, propname);@@ -527,20 +526,18 @@ int fwnode_property_match_string(const struct fwnode_handle *fwnode,if (nval ==0 )return -ENODATA ; - values =kcalloc (nval,sizeof (*values),GFP_KERNEL ); + const char **values __free(kfree) = kcalloc(nval, sizeof(*values), GFP_KERNEL);if (!values)return -ENOMEM ; ret =fwnode_property_read_string_array (fwnode, propname, values, nval);if (ret <0 ) - goto out_free; +return ret; ret =match_string (values, nval, string);if (ret <0 ) - ret = -ENODATA ; +return -ENODATA ; -out_free: -kfree (values);return ret; }EXPORT_SYMBOL_GPL (fwnode_property_match_string);
drivers/base/property.c struct fwnode_handle * · line 1129
Quoted 6 lines
drivers/base/property.c +1
-1@@ -1128,8 +1125,9 @@ struct fwnode_handle * fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode, struct fwnode_handle *prev) { - struct fwnode_handle *ep, *port_parent = NULL; const struct fwnode_handle *parent; + struct fwnode_handle *port_parent __free(fwnode_handle) = NULL;
The thing on the right-hand side of the assignment should be a constructor, shouldn't it?
Quoted 23 lines
drivers/base/property.c +3
-7+struct fwnode_handle *ep;/* * If this function is in a loop and the previous iteration returned @@ -1147,13 +1145,9 @@ fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode, ep =fwnode_call_ptr_op (parent, graph_get_next_endpoint, prev);if (ep) - goto out_put_port_parent; +return ep; - ep =fwnode_graph_get_next_endpoint (parent->secondary,NULL ); - -out_put_port_parent: -fwnode_handle_put (port_parent); -return ep; +return fwnode_graph_get_next_endpoint (parent->secondary,NULL ); }EXPORT_SYMBOL_GPL (fwnode_graph_get_next_endpoint); --2.50.1
Andy Shevchenko
reviewerRe: [PATCH v4 2/3] device property: Refactor to use RAII approach
Quoted 4 lines · Rafael J. Wysocki
Quoted 2 lines · Andy Shevchenko
In a couple of functions code can be made cleaner with help of
__free()macro. Refactor these to use RAII approach.
...
Quoted 9 lines
drivers/base/property.c struct fwnode_handle * · line 1129
Quoted 6 lines
fwnode_graph_get_next_endpoint(const struct
fwnode_handle*fwnode,
structfwnode_handle*prev)
{drivers/base/property.c +1
-1-struct fwnode_handle *ep, *port_parent =NULL ;const struct fwnode_handle *parent; + struct fwnode_handle *port_parent __free(fwnode_handle) = NULL;The thing on the right-hand side of the assignment should be a constructor, shouldn't it?
Nope. It's a recommendation to do a such, but in this case it's hard to achieve (since the constructor is inside the conditional scope) and actually is not needed as we don't mix up with other RAII calls, such a guard()().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerPATCH v4 3/3 device property: add test cases for fwnode_for_each_child_node()
In reply to Andy ShevchenkoFrom: Xu Yang <[email protected]>
Add test cases for fwnode_for_each_child_node() API.
Test command:
$ ./tools/testing/kunit/kunit.py run property-entry
--- drivers/base/test/Kconfig | 1 + drivers/base/test/property-entry-test.c | 136 ++++++++++++++++++++++++ 2 files changed, 137 insertions(+)
drivers/base/test/Kconfig +1 -0
@@ -17,4 +17,5 @@ config DM_KUNIT_TEST config DRIVER_PE_KUNIT_TEST tristate "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS depends on KUNIT + select OF default KUNIT_ALL_TESTS
drivers/base/test/property-entry-test.c +136 -0
154 lines not on this page · raw message
Signature · 1 line
2.50.1
Danilo Krummrich
maintainerRe: [PATCH v4 3/3] device property: add test cases for fwnode_for_each_child_node()
(Cc: OF)
Quoted 9 lines · Andy Shevchenko
From: Xu Yang <[email protected]>
Add test cases for
fwnode_for_each_child_node()API.Test command:
$ ./tools/testing/kunit/kunit.py run property-entrySigned-off-by Xu Yang <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
This patch broke the build [1], hence I dropped it from the queue (but kept the first two patches of this series).
of_node_init() can't be used from modules, either it needs EXPORT_SYMBOL_GPL() itself, or of_node_ktype needs to be exported for this kunit test to work.
Andy Shevchenko
reviewerRe: [PATCH v4 3/3] device property: add test cases for fwnode_for_each_child_node()
Quoted 7 lines · Danilo Krummrich
Quoted 6 lines · Andy Shevchenko
Add test cases for
fwnode_for_each_child_node()API.Test command:
$ ./tools/testing/kunit/kunit.py run property-entry
Quoted 8 lines
Quoted 2 lines
Signed-off-by Xu Yang <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>This patch broke the build [1], hence I dropped it from the queue (but kept the first two patches of this series).
of_node_init()can't be used from modules, either it needsEXPORT_SYMBOL_GPL()itself, orof_node_ktypeneeds to be exported for this kunit test to work.
Oh, thanks for pointing out.
Quoted 1 line
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 3/3] device property: add test cases for fwnode_for_each_child_node()
Hi Danilo and Andy,
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Danilo Krummrich
Quoted 8 lines · Andy Shevchenko
Add test cases for
fwnode_for_each_child_node()API.Test command:
$ ./tools/testing/kunit/kunit.py run property-entrySigned-off-by Xu Yang <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
Quoted 7 lines
Quoted 5 lines
This patch broke the build [1], hence I dropped it from the queue (but kept the first two patches of this series).
of_node_init()can't be used from modules, either it needsEXPORT_SYMBOL_GPL()itself, orof_node_ktypeneeds to be exported for this kunit test to work.Oh, thanks for pointing out.
Fix patches are sent:
https://lore.kernel.org/imx/[email protected]/
Thanks,
Xu Yang
Hi Greg and Danilo,
Quoted 15 lines · Andy Shevchenko
This series fixes two issues in the fwnode child iteration logic when a secondary fwnode is present.
The first issue is a refcount imbalance in
software_node_get_next_child(). When a software node is used as a secondary fwnode, the iteration code may incorrectly decrement the refcount of child nodes that do not belong to the software node hierarchy. This results in refcount underflow and possible use-after-free.The second issue is an infinite loop in
fwnode_for_each_child_node(), caused by improper handling of iteration state across primary and secondary fwnodes. When iterating over children from both primary and secondary fwnodes, the code may incorrectly resume iteration from the primary fwnode even when the current child belongs to the secondary, leading to repeated traversal and a loop.
Quoted 3 lines
Both issues are triggered when mixing different fwnode types through the secondary mechanism, and stem from incorrect assumptions about ownership and traversal context of child nodes.
Gentle ping on this series.
Thanks,
Xu Yang
Quoted 31 lines
Changes in v4:
- amended the fix and test case (Andy)
- added patch 2 to align other implementations with RAII approach (Andy)
- tested on Intel Galileo board for which the initial code was developed (Andy)
- Link to v3: https://patch.msgid.link/[email protected]Changes in v3:
- remove software node patch
- add a kunit test case suggested by Andy Shevchenko
- Link to v2: https://patch.msgid.link/[email protected]Changes in v2:
- use__free()to cleanup parent fwnode
- Link to v1: https://lore.kernel.org/r/[email protected]Andy Shevchenko (1):
device property: Refactor to use RAII approachXu Yang (2):
device property: fix infinite loop infwnode_for_each_child_node()device property: add test cases forfwnode_for_each_child_node()drivers/base/property.c | 41 ++++--- drivers/base/test/Kconfig | 1 + drivers/base/test/property-entry-test.c | 136 ++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 17 deletions(-)Signature · 1 line
2.50.1