of: overlay: fix device_node leak when attaching node to changeset fails
PATCH 1/1 of: overlay: fix device_node leak when attaching node to changeset fails
In add_changeset_node(), when creating a new target child node via __of_node_dup(), tchild is initialized with a reference count of 1. If of_changeset_attach_node() subsequently fails, add_changeset_node() returns the error without calling of_node_put(tchild), leaking the allocated device_node.
Furthermore, because OF_OVERLAY is set on tchild before of_changeset_attach_node() is called, calling of_node_put(tchild) when tchild was never added to the changeset (and thus never had OF_OVERLAY_FREE_CSET set by __of_changeset_entry_destroy()) would cause of_node_release() to log a premature refcount zero error and skip freeing the node.
Move of_node_set_flag(tchild, OF_OVERLAY) after of_changeset_attach_node() succeeds and call of_node_put(tchild) on the failure path, matching of_changeset_create_node().
--- drivers/of/overlay.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/of/overlay.c +5 -3
@@ -442,11 +442,13 @@ static int add_changeset_node(struct overlay_changeset *ovcs,if (phandle && (size ==4 )) tchild->phandle =be32_to_cpup (phandle); -of_node_set_flag (tchild,OF_OVERLAY ); - ret =of_changeset_attach_node (&ovcs->cset, tchild); -if (ret) +if (ret) { +of_node_put (tchild);return ret; + } + +of_node_set_flag (tchild,OF_OVERLAY ); target_child.np = tchild; target_child.in_livetree = false;
Signature · 1 line
2.56.0
Quoted 13 lines · Selby Mashiki
In
add_changeset_node(), when creating a new target child node via__of_node_dup(), tchild is initialized with a reference count of 1. Ifof_changeset_attach_node()subsequently fails,add_changeset_node()returns the error without callingof_node_put(tchild), leaking the allocateddevice_node.Furthermore, because
OF_OVERLAYis set on tchild beforeof_changeset_attach_node()is called, callingof_node_put(tchild)when tchild was never added to the changeset (and thus never hadOF_OVERLAY_FREE_CSETset by__of_changeset_entry_destroy()) would causeof_node_release()to log a premature refcount zero error and skip freeing the node.
Quoted 3 lines
Move
of_node_set_flag(tchild, OF_OVERLAY)afterof_changeset_attach_node()succeeds and callof_node_put(tchild)on the failure path, matchingof_changeset_create_node().
How did you reproduce it? Looks like LLM written log.
Best regards,
Krzysztof