Skip to content

Commit 7cce81d

Browse files
Weigang Hemiquelraynal
authored andcommitted
mtd: parsers: ofpart: fix OF node refcount leak in parse_fixed_partitions()
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>
1 parent 68cd8ef commit 7cce81d

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

drivers/mtd/parsers/ofpart_core.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static int parse_fixed_partitions(struct mtd_info *master,
8181
of_id = of_match_node(parse_ofpart_match_table, ofpart_node);
8282
if (dedicated && !of_id) {
8383
/* The 'partitions' subnode might be used by another parser */
84+
of_node_put(ofpart_node);
8485
return 0;
8586
}
8687

@@ -95,12 +96,18 @@ static int parse_fixed_partitions(struct mtd_info *master,
9596
nr_parts++;
9697
}
9798

98-
if (nr_parts == 0)
99+
if (nr_parts == 0) {
100+
if (dedicated)
101+
of_node_put(ofpart_node);
99102
return 0;
103+
}
100104

101105
parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
102-
if (!parts)
106+
if (!parts) {
107+
if (dedicated)
108+
of_node_put(ofpart_node);
103109
return -ENOMEM;
110+
}
104111

105112
i = 0;
106113
for_each_child_of_node(ofpart_node, pp) {
@@ -179,6 +186,9 @@ static int parse_fixed_partitions(struct mtd_info *master,
179186
if (quirks && quirks->post_parse)
180187
quirks->post_parse(master, parts, nr_parts);
181188

189+
if (dedicated)
190+
of_node_put(ofpart_node);
191+
182192
*pparts = parts;
183193
return nr_parts;
184194

@@ -187,6 +197,8 @@ static int parse_fixed_partitions(struct mtd_info *master,
187197
master->name, pp, mtd_node);
188198
ret = -EINVAL;
189199
ofpart_none:
200+
if (dedicated)
201+
of_node_put(ofpart_node);
190202
of_node_put(pp);
191203
kfree(parts);
192204
return ret;

0 commit comments

Comments
 (0)