public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions()
@ 2026-01-23  5:26 Weigang He
  2026-01-23 11:50 ` Markus Elfring
  2026-01-29 19:12 ` Miquel Raynal
  0 siblings, 2 replies; 3+ messages in thread
From: Weigang He @ 2026-01-23  5:26 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, Weigang He

of_get_child_by_name() returns a node pointer with refcount incremented,
which must be released with of_node_put() when done. However, in
parse_fixed_partitions(), when dedicated is true (i.e., a "partitions"
subnode was found), the ofpart_node obtained from of_get_child_by_name()
is never released on any code path.

Add of_node_put(ofpart_node) calls on all exit paths when dedicated is
true to fix the reference count leak.

This bug was detected by our static analysis tool.

Fixes: 562b4e91d3b2 ("mtd: parsers: ofpart: fix parsing subpartitions")
Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
---
 drivers/mtd/parsers/ofpart_core.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/parsers/ofpart_core.c b/drivers/mtd/parsers/ofpart_core.c
index abfa687989182..09961c6f39496 100644
--- a/drivers/mtd/parsers/ofpart_core.c
+++ b/drivers/mtd/parsers/ofpart_core.c
@@ -77,6 +77,7 @@ static int parse_fixed_partitions(struct mtd_info *master,
 	of_id = of_match_node(parse_ofpart_match_table, ofpart_node);
 	if (dedicated && !of_id) {
 		/* The 'partitions' subnode might be used by another parser */
+		of_node_put(ofpart_node);
 		return 0;
 	}
 
@@ -91,12 +92,18 @@ static int parse_fixed_partitions(struct mtd_info *master,
 		nr_parts++;
 	}
 
-	if (nr_parts == 0)
+	if (nr_parts == 0) {
+		if (dedicated)
+			of_node_put(ofpart_node);
 		return 0;
+	}
 
 	parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
-	if (!parts)
+	if (!parts) {
+		if (dedicated)
+			of_node_put(ofpart_node);
 		return -ENOMEM;
+	}
 
 	i = 0;
 	for_each_child_of_node(ofpart_node,  pp) {
@@ -175,6 +182,9 @@ static int parse_fixed_partitions(struct mtd_info *master,
 	if (quirks && quirks->post_parse)
 		quirks->post_parse(master, parts, nr_parts);
 
+	if (dedicated)
+		of_node_put(ofpart_node);
+
 	*pparts = parts;
 	return nr_parts;
 
@@ -183,6 +193,8 @@ static int parse_fixed_partitions(struct mtd_info *master,
 	       master->name, pp, mtd_node);
 	ret = -EINVAL;
 ofpart_none:
+	if (dedicated)
+		of_node_put(ofpart_node);
 	of_node_put(pp);
 	kfree(parts);
 	return ret;
-- 
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions()
  2026-01-23  5:26 [PATCH] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions() Weigang He
@ 2026-01-23 11:50 ` Markus Elfring
  2026-01-29 19:12 ` Miquel Raynal
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2026-01-23 11:50 UTC (permalink / raw)
  To: Weigang He, linux-mtd, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra
  Cc: LKML, kernel-janitors

…
> Add of_node_put(ofpart_node) calls on all exit paths when dedicated is
> true to fix the reference count leak.
> 
> This bug was detected by our static analysis tool.

Will any additional background information become more helpful here?

Regards,
Markus

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions()
  2026-01-23  5:26 [PATCH] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions() Weigang He
  2026-01-23 11:50 ` Markus Elfring
@ 2026-01-29 19:12 ` Miquel Raynal
  1 sibling, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2026-01-29 19:12 UTC (permalink / raw)
  To: richard, vigneshr, Weigang He; +Cc: linux-mtd, linux-kernel

On Fri, 23 Jan 2026 05:26:08 +0000, Weigang He wrote:
> of_get_child_by_name() returns a node pointer with refcount incremented,
> which must be released with of_node_put() when done. However, in
> parse_fixed_partitions(), when dedicated is true (i.e., a "partitions"
> subnode was found), the ofpart_node obtained from of_get_child_by_name()
> is never released on any code path.
> 
> Add of_node_put(ofpart_node) calls on all exit paths when dedicated is
> true to fix the reference count leak.
> 
> [...]

Applied to mtd/next, thanks!

[1/1] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions()
      commit: 7cce81df7d26d44123bd7620715c8349d96793d7

Patche(s) should be available on mtd/linux.git and will be
part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-01-29 19:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-23  5:26 [PATCH] mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions() Weigang He
2026-01-23 11:50 ` Markus Elfring
2026-01-29 19:12 ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox