Skip to content
/ linux Public

Commit f016288

Browse files
Weigang HeSasha Levin
authored andcommitted
mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions()
[ Upstream commit 7cce81d ] 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: 562b4e9 ("mtd: parsers: ofpart: fix parsing subpartitions") Signed-off-by: Weigang He <geoffreyhe2@gmail.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 738fba2 commit f016288

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

drivers/mtd/parsers/ofpart_core.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static int parse_fixed_partitions(struct mtd_info *master,
7777
of_id = of_match_node(parse_ofpart_match_table, ofpart_node);
7878
if (dedicated && !of_id) {
7979
/* The 'partitions' subnode might be used by another parser */
80+
of_node_put(ofpart_node);
8081
return 0;
8182
}
8283

@@ -91,12 +92,18 @@ static int parse_fixed_partitions(struct mtd_info *master,
9192
nr_parts++;
9293
}
9394

94-
if (nr_parts == 0)
95+
if (nr_parts == 0) {
96+
if (dedicated)
97+
of_node_put(ofpart_node);
9598
return 0;
99+
}
96100

97101
parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
98-
if (!parts)
102+
if (!parts) {
103+
if (dedicated)
104+
of_node_put(ofpart_node);
99105
return -ENOMEM;
106+
}
100107

101108
i = 0;
102109
for_each_child_of_node(ofpart_node, pp) {
@@ -175,6 +182,9 @@ static int parse_fixed_partitions(struct mtd_info *master,
175182
if (quirks && quirks->post_parse)
176183
quirks->post_parse(master, parts, nr_parts);
177184

185+
if (dedicated)
186+
of_node_put(ofpart_node);
187+
178188
*pparts = parts;
179189
return nr_parts;
180190

@@ -183,6 +193,8 @@ static int parse_fixed_partitions(struct mtd_info *master,
183193
master->name, pp, mtd_node);
184194
ret = -EINVAL;
185195
ofpart_none:
196+
if (dedicated)
197+
of_node_put(ofpart_node);
186198
of_node_put(pp);
187199
kfree(parts);
188200
return ret;

0 commit comments

Comments
 (0)