Skip to contentSkip to conversation

iommu: fix resource leak in iommu_group_alloc

PATCH New Yuan Can 1 participant 1 message Aug 29, 2022 IOMMU

lore
Share & tools
lore

Yuan Can

author
#0 lore raw

PATCH 1/1 iommu: fix resource leak in iommu_group_alloc

When it fails to create file for reserved_regions and type of an iommu_group, the iommu_group_alloc returns ERR_PTR without releasing the iommu_group.
Add error handling to cleanup leaking resources.

---
 drivers/iommu/iommu.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/iommu/iommu.c +9 -2
index 1ca59afc8472..fff23231b5fc 100644
@@ -675,15 +675,22 @@ struct iommu_group *iommu_group_alloc(void)
 	ret = iommu_group_create_file(group,
 				      &iommu_group_attr_reserved_regions);
 	if (ret)
-		return ERR_PTR(ret);
+		goto out_put_group;
 
 	ret = iommu_group_create_file(group, &iommu_group_attr_type);
 	if (ret)
-		return ERR_PTR(ret);
+		goto out_remove_file;
 
 	pr_debug("Allocated group %d\n", group->id);
 
 	return group;
+
+out_remove_file:
+	iommu_group_remove_file(group, &iommu_group_attr_reserved_regions);
+out_put_group:
+	iommu_group_put(group);
+
+	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(iommu_group_alloc);
 
Signature · 1 line
2.17.1