mtd: virt_concat: Fix device_node reference leaks in mtd_virt_concat_create_item()
Subsystems (2)
PATCH 1/1 mtd: virt_concat: Fix device_node reference leaks in mtd_virt_concat_create_item()
In mtd_virt_concat_create_item(), several device_node reference count leaks and lifecycle bugs exist:
1. of_parse_phandle() returns a device_node with its refcount incremented. The validation loop calls mtd_is_part_concat() directly on the returned pointer without releasing it, leaking the node reference on every iteration and on early return.
2. item->nodes[0] is assigned directly from parts without acquiring a reference via of_node_get(). However, mtd_virt_concat_destroy_items() and mtd_virt_concat_destroy() call of_node_put() on all item->nodes, and mtd_virt_concat_node_create() drops its own reference on parts in the next iteration of of_find_node_with_property(), leading to an underflow / premature release of parts.
3. If kzalloc_flex() fails for concat, the error path frees item without releasing the acquired references in item->nodes[0..count-1].
Fix these issues by releasing next after mtd_is_part_concat() in the validation loop, taking a reference with of_node_get() when assigning item->nodes[0], and releasing all acquired node references if concat allocation fails.
--- drivers/mtd/mtd_virt_concat.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/mtd/mtd_virt_concat.c +8 -2
@@ -163,7 +163,11 @@ static int mtd_virt_concat_create_item(struct device_node *parts,int i;for (i =0 ; i < (count -1 ); i++) { -if (mtd_is_part_concat (of_parse_phandle (parts,CONCAT_PROP , i))) +struct device_node *next =of_parse_phandle (parts,CONCAT_PROP , i); +bool is_concat =mtd_is_part_concat (next); + +of_node_put (next); +if (is_concat)return 0 ; }
@@ -178,13 +182,15 @@ static int mtd_virt_concat_create_item(struct device_node *parts,* is defined is the first device in the list of concat * devices. */ - item->nodes[0 ] = parts; + item->nodes[0 ] =of_node_get (parts);for (i =1 ; i < count; i++) item->nodes[i] =of_parse_phandle (parts,CONCAT_PROP , (i -1 )); concat =kzalloc_flex (*concat, subdev, count);if (!concat) { +for (i =0 ; i < count; i++) +of_node_put (item->nodes[i]);kfree (item);return -ENOMEM ; }
Signature · 1 line
2.53.0
Quoted 9 lines · Vishal Satle
In
mtd_virt_concat_create_item(), severaldevice_nodereference count leaks and lifecycle bugs exist:1.
of_parse_phandle()returns adevice_nodewith its refcount incremented. The validation loop callsmtd_is_part_concat()directly on the returned pointer without releasing it, leaking the node reference on every iteration and on early return.[...]
Applied to mtd/next, thanks!
[1/1] mtd: virt_concat: Fix device_node reference leaks in mtd_virt_concat_create_item()
commit: d2a681eed8154900ed66c4f77b32d860b71d44cc
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