[PATCH 1/3] partitions: dos: fix memory leaks

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Nov 25 07:27:54 PST 2024


These leaks don't bother us normally, but when fuzzing they quickly add
up, so let's fix them.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/partitions/dos.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index a388a23eed0d..5a055efec7d1 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -20,9 +20,16 @@
 #include <linux/err.h>
 #include <partitions.h>
 
+struct disk_signature_priv {
+	uint32_t signature;
+	struct block_device *blk;
+	struct param_d *param;
+};
+
 struct dos_partition_desc {
 	struct partition_desc pd;
 	uint32_t signature;
+	struct disk_signature_priv disksig;
 };
 
 struct dos_partition {
@@ -76,11 +83,6 @@ static int write_mbr(struct block_device *blk, void *buf)
 	return block_flush(blk);
 }
 
-struct disk_signature_priv {
-	uint32_t signature;
-	struct block_device *blk;
-};
-
 static int dos_set_disk_signature(struct param_d *p, void *_priv)
 {
 	struct disk_signature_priv *priv = _priv;
@@ -273,7 +275,7 @@ static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 	if (extended_partition)
 		dos_extended_partition(blk, dpd, extended_partition, signature);
 
-	dsp = xzalloc(sizeof(*dsp));
+	dsp = &dpd->disksig;
 	dsp->blk = blk;
 
 	/*
@@ -286,7 +288,7 @@ static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 	 * signature and pp is a zero-filled hex representation of the 1-based
 	 * partition number.
 	 */
-	dev_add_param_uint32(blk->dev, "nt_signature",
+	dsp->param = dev_add_param_uint32(blk->dev, "nt_signature",
 			dos_set_disk_signature, dos_get_disk_signature,
 			&dsp->signature, "%08x", dsp);
 
@@ -295,6 +297,8 @@ static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 
 static void dos_partition_free(struct partition_desc *pd)
 {
+	struct dos_partition_desc *dpd
+		= container_of(pd, struct dos_partition_desc, pd);
 	struct partition *part, *tmp;
 
 	list_for_each_entry_safe(part, tmp, &pd->partitions, list) {
@@ -303,6 +307,8 @@ static void dos_partition_free(struct partition_desc *pd)
 		free(dpart);
 	}
 
+	dev_remove_param(dpd->disksig.param);
+
 	free(pd);
 }
 
@@ -414,12 +420,16 @@ static __maybe_unused int dos_partition_mkpart(struct partition_desc *pd,
 	dpart = xzalloc(sizeof(*dpart));
 	part = &dpart->part;
 
-	if (start_lba > UINT_MAX)
+	if (start_lba > UINT_MAX) {
+		free(dpart);
 		return -ENOSPC;
+	}
 	size = end_lba - start_lba + 1;
 
-	if (size > UINT_MAX)
+	if (size > UINT_MAX) {
+		free(dpart);
 		return -ENOSPC;
+	}
 
 	dpart->type = fs_type_to_type(fs_type);
 
-- 
2.39.5




More information about the barebox mailing list