Skip to contentSkip to conversation

UBIFS: fix memory leak on error path

PATCH In review Sidney Amani 2 participants 6 messages May 17, 2012 – May 20, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 UBIFS: fix memory leak on error path

UBIFS leaks memory on error path in 'mount_ubifs()'. In case of failure in 'ubifs_lpt_init()' or 'ubifs_fixup_free_space()', it does not call 'ubifs_lpt_free()' whereas LPT data structures can potentially be allocated. The amount of memory leaked can be quite high -- see 'ubifs_lpt_init()'.

The bug was introduced when moving the LPT initialisation earlier in the mount process (commit '781c5717a95a74b294beb38b8276943b0f8b5bb4').

---
 fs/ubifs/super.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
fs/ubifs/super.c +2 -2
index 76e4e05..50216ec 100644
@@ -1296,12 +1296,12 @@ static int mount_ubifs(struct ubifs_info *c)
 
 	err = ubifs_lpt_init(c, 1, !c->ro_mount);
 	if (err)
-		goto out_master;
+		goto out_lpt;
 
 	if (!c->ro_mount && c->space_fixup) {
 		err = ubifs_fixup_free_space(c);
 		if (err)
-			goto out_master;
+			goto out_lpt;
 	}
 
 	if (!c->ro_mount) {
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/2 UBIFS: make ubifs_lpt_init clean-up in case of failure

From: Artem Bityutskiy <[email protected]>

Most functions in UBIFS follow the following designn pattern: if the function allocates multiple resources, and failss at some point, it frees what it has allocated and returns an error. So the caller can rely on the fact that the callee has cleaned up everything after own failure.

---
 fs/ubifs/lpt.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/ubifs/lpt.c +6 -2
index 2054e81..b4280c4 100644
@@ -1740,16 +1740,20 @@ int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
 	if (rd) {
 		err = lpt_init_rd(c);
 		if (err)
-			return err;
+			goto out_err;
 	}
 
 	if (wr) {
 		err = lpt_init_wr(c);
 		if (err)
-			return err;
+			goto out_err;
 	}
 
 	return 0;
+
+out_err:
+	ubifs_lpt_free(c, 0);
+	return err;
 }
 
 /**
Signature · 1 line
1.7.10
#2 lore raw

PATCH 2/2 UBIFS: fix memory leak on error path

From: Sidney Amani <[email protected]>

UBIFS leaks memory on error path in 'mount_ubifs()'. In case of failure in 'ubifs_fixup_free_space()', it does not call 'ubifs_lpt_free()' whereas LPT data structures can potentially be allocated. The amount of memory leaked can be quite high -- see 'ubifs_lpt_init()'.

The bug was introduced when moving the LPT initialisation earlier in the mount process (commit '781c5717a95a74b294beb38b8276943b0f8b5bb4').

---
 fs/ubifs/super.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ubifs/super.c +1 -1
index 5b30c4d..675b781 100644
@@ -1301,7 +1301,7 @@ static int mount_ubifs(struct ubifs_info *c)
 	if (!c->ro_mount && c->space_fixup) {
 		err = ubifs_fixup_free_space(c);
 		if (err)
-			goto out_master;
+			goto out_lpt;
 	}
 
 	if (!c->ro_mount) {
Signature · 1 line
1.7.10
author Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] UBIFS: make ubifs_lpt_init clean-up in case of failure

In reply to Artem Bityutskiy
Quoted 8 lines · Artem Bityutskiy

From: Artem Bityutskiy <[email protected]>

Most functions in UBIFS follow the following designn pattern: if the function allocates multiple resources, and failss at some point, it frees what it has allocated and returns an error. So the caller can rely on the fact that the callee has cleaned up everything after own failure.

Signature · 1 line
Sidney
#4 lore raw
In reply to Sidney Amani
Quoted 10 lines · Sidney Amani

UBIFS leaks memory on error path in 'mount_ubifs()'. In case of failure in 'ubifs_lpt_init()' or 'ubifs_fixup_free_space()', it does not call 'ubifs_lpt_free()' whereas LPT data structures can potentially be allocated. The amount of memory leaked can be quite high -- see 'ubifs_lpt_init()'.

The bug was introduced when moving the LPT initialisation earlier in the mount process (commit '781c5717a95a74b294beb38b8276943b0f8b5bb4').

I've replied to you with the counter-proposal patches which I think should fix this issue in a bit better way. Please, take a look - If you are fine with that, I can push it.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#5 lore raw
Quoted 4 lines · Artem Bityutskiy

I've replied to you with the counter-proposal patches which I think should fix this issue in a bit better way. Please, take a look - If you are fine with that, I can push it.

Agreed, your set of patches is better.

Cheers

Signature · 1 line
Sidney