Skip to contentSkip to conversation

of: unittest: fix use-after-free in of_unittest_changeset()

PATCH Applied Wentao Liang 2 participants 2 messages Apr 9 – Apr 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of: unittest: fix use-after-free in of_unittest_changeset()

The variable 'parent' is assigned the value of 'nchangeset' earlier in the function, meaning both point to the same struct device_node. The call to of_node_put(nchangeset) can decrement the reference count to zero and free the node if there are no other holders. After that, the code still uses 'parent' to check for the presence of a property and to read a string property, leading to a use-after-free.

Fix this by moving the of_node_put() call after the last access to 'parent', avoiding the UAF.

Fixes 1c668ea65506 ("of: unittest: Use of_property_present()")
---
 drivers/of/unittest.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/of/unittest.c +1 -2
index 2940295843e6..eae7ebdf5130 100644
@@ -896,8 +896,6 @@ static void __init of_unittest_changeset(void)
 
 	unittest(!of_changeset_apply(&chgset), "apply failed\n");
 
-	of_node_put(nchangeset);
-
 	/* Make sure node names are constructed correctly */
 	unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
 		 "'%pOF' not added\n", n21);
@@ -919,6 +917,7 @@ static void __init of_unittest_changeset(void)
 	if (!ret)
 		unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
 
+	of_node_put(nchangeset);
 	of_changeset_destroy(&chgset);
 
 	of_node_put(n1);
Signature · 1 line
2.34.1
maintainer Applied
#1 lore raw
Quoted 10 lines · Wentao Liang

The variable 'parent' is assigned the value of 'nchangeset' earlier in the function, meaning both point to the same struct device_node. The call to of_node_put(nchangeset) can decrement the reference count to zero and free the node if there are no other holders. After that, the code still uses 'parent' to check for the presence of a property and to read a string property, leading to a use-after-free.

Fix this by moving the of_node_put() call after the last access to 'parent', avoiding the UAF.

Quoted 7 lines
Fixes 1c668ea65506 ("of: unittest: Use of_property_present()")
---
 drivers/of/unittest.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Applied, thanks!