Commit d6ce4f0e authored by Ruan Jinjie's avatar Ruan Jinjie Committed by Rob Herring

of: unittest: fix null pointer dereferencing in of_unittest_find_node_by_name()

when kmalloc() fail to allocate memory in kasprintf(), name
or full_name will be NULL, strcmp() will cause
null pointer dereference.

Fixes: 0d638a07 ("of: Convert to using %pOF instead of full_name")
Signed-off-by: default avatarRuan Jinjie <ruanjinjie@huawei.com>
Link: https://lore.kernel.org/r/20230727080246.519539-1-ruanjinjie@huawei.comSigned-off-by: default avatarRob Herring <robh@kernel.org>
parent 3e773081
...@@ -77,7 +77,7 @@ static void __init of_unittest_find_node_by_name(void) ...@@ -77,7 +77,7 @@ static void __init of_unittest_find_node_by_name(void)
np = of_find_node_by_path("/testcase-data"); np = of_find_node_by_path("/testcase-data");
name = kasprintf(GFP_KERNEL, "%pOF", np); name = kasprintf(GFP_KERNEL, "%pOF", np);
unittest(np && !strcmp("/testcase-data", name), unittest(np && name && !strcmp("/testcase-data", name),
"find /testcase-data failed\n"); "find /testcase-data failed\n");
of_node_put(np); of_node_put(np);
kfree(name); kfree(name);
...@@ -88,14 +88,14 @@ static void __init of_unittest_find_node_by_name(void) ...@@ -88,14 +88,14 @@ static void __init of_unittest_find_node_by_name(void)
np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
name = kasprintf(GFP_KERNEL, "%pOF", np); name = kasprintf(GFP_KERNEL, "%pOF", np);
unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name), unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
"find /testcase-data/phandle-tests/consumer-a failed\n"); "find /testcase-data/phandle-tests/consumer-a failed\n");
of_node_put(np); of_node_put(np);
kfree(name); kfree(name);
np = of_find_node_by_path("testcase-alias"); np = of_find_node_by_path("testcase-alias");
name = kasprintf(GFP_KERNEL, "%pOF", np); name = kasprintf(GFP_KERNEL, "%pOF", np);
unittest(np && !strcmp("/testcase-data", name), unittest(np && name && !strcmp("/testcase-data", name),
"find testcase-alias failed\n"); "find testcase-alias failed\n");
of_node_put(np); of_node_put(np);
kfree(name); kfree(name);
...@@ -106,7 +106,7 @@ static void __init of_unittest_find_node_by_name(void) ...@@ -106,7 +106,7 @@ static void __init of_unittest_find_node_by_name(void)
np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
name = kasprintf(GFP_KERNEL, "%pOF", np); name = kasprintf(GFP_KERNEL, "%pOF", np);
unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name), unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
"find testcase-alias/phandle-tests/consumer-a failed\n"); "find testcase-alias/phandle-tests/consumer-a failed\n");
of_node_put(np); of_node_put(np);
kfree(name); kfree(name);
...@@ -1533,6 +1533,8 @@ static void attach_node_and_children(struct device_node *np) ...@@ -1533,6 +1533,8 @@ static void attach_node_and_children(struct device_node *np)
const char *full_name; const char *full_name;
full_name = kasprintf(GFP_KERNEL, "%pOF", np); full_name = kasprintf(GFP_KERNEL, "%pOF", np);
if (!full_name)
return;
if (!strcmp(full_name, "/__local_fixups__") || if (!strcmp(full_name, "/__local_fixups__") ||
!strcmp(full_name, "/__fixups__")) { !strcmp(full_name, "/__fixups__")) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment