[PATCH v2] firewire: core: fix possible memory leak in build_tree()

Abdun Nihaal <[email protected]> Sat, 1 Aug 2026 16:39:13 +0530
Newsgroups gmane.linux.kernel,gmane.linux.kernel.firewire.devel,gmane.linux.kernel.stable
Message-ID <[email protected]>
The memory allocated for node in fw_node_create() is not freed when a
parent port inconsistency error occurs. Fix that by invalidating
references to the node, and then calling fw_node_put() on it.

Fixes: 3038e353cfaf ("firewire: Add core firewire stack.")
Cc: [email protected]
Signed-off-by: Abdun Nihaal <[email protected]>
---
Compile tested only. Issue found using static analysis

v1->v2:
- Invalidate references to the node in the child nodes,
  as suggested by Takashi Sakamoto.
- Use fw_node_put() instead of kfree() for consistency

v1 link: https://lore.kernel.org/all/[email protected]/

 drivers/firewire/core-topology.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/firewire/core-topology.c b/drivers/firewire/core-topology.c
index df2ac0dab106..3d0109c3ed90 100644
--- a/drivers/firewire/core-topology.c
+++ b/drivers/firewire/core-topology.c
@@ -227,6 +227,15 @@ static struct fw_node *build_tree(struct fw_card *card, const u32 *sid, int self
 		    (enumerator.quadlet_count > 0 && parent_count != 1)) {
 			fw_err(card, "parent port inconsistency for node %d: "
 			       "parent_count=%d\n", phy_id, parent_count);
+			while (h != &stack) {
+				child = fw_node(h);
+				for (i = 0; i < child->port_count; i++) {
+					if (child->ports[i] == node)
+						child->ports[i] = NULL;
+				}
+				h = h->next;
+			}
+			fw_node_put(node);
 			return NULL;
 		}
 
-- 
2.43.0