Skip to contentSkip to conversation

btrfs-progs: fix leak of "path" in btrfs_find_item() error paths

PATCH Applied Eryu Guan 2 participants 19 messages Oct 19, 2015 – Oct 21, 2015 Btrfs file system

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/10 btrfs-progs: fix leak of "path" in btrfs_find_item() error paths

path needs to be freed before return.

---
 ctree.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
ctree.c +11 -9
index e6e5689..1434007 100644
@@ -1058,26 +1058,28 @@ int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
 		path = found_path;
 
 	ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
-	if ((ret < 0) || (found_key == NULL)) {
-		if (path != found_path)
-			btrfs_free_path(path);
-		return ret;
-	}
+	if ((ret < 0) || (found_key == NULL))
+		goto out;
 
 	eb = path->nodes[0];
 	if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
 		ret = btrfs_next_leaf(fs_root, path);
 		if (ret)
-			return ret;
+			goto out;
 		eb = path->nodes[0];
 	}
 
 	btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
 	if (found_key->type != key.type ||
-			found_key->objectid != key.objectid)
-		return 1;
+			found_key->objectid != key.objectid) {
+		ret = 1;
+		goto out;
+	}
 
-	return 0;
+out:
+	if (path != found_path)
+		btrfs_free_path(path);
+	return ret;
 }
 
 /*
Signature · 1 line
2.4.3
author
#1 lore raw

PATCH 2/10 btrfs-progs: save and return error number correctly in check_chunks_and_extents

"err" is assigned to "ret" then "ret" gets overwritten by check_extent_refs() before "ret" can be used.

---
 cmds-check.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
cmds-check.c +6 -7
index 4225b21..80a7c29 100644
@@ -8066,13 +8066,12 @@ again:
 		goto out;
 	}
 
-	err = check_chunks(&chunk_cache, &block_group_cache,
+	ret = check_chunks(&chunk_cache, &block_group_cache,
 			   &dev_extent_cache, NULL, NULL, NULL, 0);
-	if (err) {
-		if (err == -EAGAIN)
+	if (ret) {
+		if (ret == -EAGAIN)
 			goto loop;
-		if (!ret)
-			ret = err;
+		err = ret;
 	}
 
 	ret = check_extent_refs(root, &extent_cache);
@@ -8082,8 +8081,8 @@ again:
 		goto out;
 	}
 
-	err = check_devices(&dev_cache, &dev_extent_cache);
-	if (err && !ret)
+	ret = check_devices(&dev_cache, &dev_extent_cache);
+	if (ret && err)
 		ret = err;
 
 out:
Signature · 1 line
2.4.3
author
#2 lore raw

PATCH 2/10 btrfs-progs: save error number correctly in check_chunks_and_extents

In reply to Eryu Guan

Coverity reports assigning value from "err" to "ret", but that stored value is overwritten by check_extent_refs() before it can be used.

---
 cmds-check.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
cmds-check.c +6 -7
index 4225b21..80a7c29 100644
@@ -8066,13 +8066,12 @@ again:
 		goto out;
 	}
 
-	err = check_chunks(&chunk_cache, &block_group_cache,
+	ret = check_chunks(&chunk_cache, &block_group_cache,
 			   &dev_extent_cache, NULL, NULL, NULL, 0);
-	if (err) {
-		if (err == -EAGAIN)
+	if (ret) {
+		if (ret == -EAGAIN)
 			goto loop;
-		if (!ret)
-			ret = err;
+		err = ret;
 	}
 
 	ret = check_extent_refs(root, &extent_cache);
@@ -8082,8 +8081,8 @@ again:
 		goto out;
 	}
 
-	err = check_devices(&dev_cache, &dev_extent_cache);
-	if (err && !ret)
+	ret = check_devices(&dev_cache, &dev_extent_cache);
+	if (ret && err)
 		ret = err;
 
 out:
Signature · 1 line
2.4.3
#3 lore raw

Re: [PATCH 02/10] btrfs-progs: save error number correctly in check_chunks_and_extents

Quoted 2 lines · Eryu Guan

Coverity reports assigning value from "err" to "ret", but that stored value is overwritten by check_extent_refs() before it can be used.

If you fix a coverity issue, please add a tag and the id, like

Resolves-coverity-id: 1234

author
#4 lore raw

Re: [PATCH 02/10] btrfs-progs: save error number correctly in check_chunks_and_extents

Quoted 7 lines · David Sterba
Quoted 2 lines · Eryu Guan

Coverity reports assigning value from "err" to "ret", but that stored value is overwritten by check_extent_refs() before it can be used.

If you fix a coverity issue, please add a tag and the id, like

Resolves-coverity-id: 1234

I was looking hard for CID but without luck.. I will ask around and see where I can find the CIDs.

Thanks,
Eryu

#5 lore raw

Re: [PATCH 02/10] btrfs-progs: save error number correctly in check_chunks_and_extents

Quoted 11 lines · Eryu Guan
Quoted 7 lines · David Sterba
Quoted 2 lines · Eryu Guan

Coverity reports assigning value from "err" to "ret", but that stored value is overwritten by check_extent_refs() before it can be used.

If you fix a coverity issue, please add a tag and the id, like

Resolves-coverity-id: 1234

I was looking hard for CID but without luck.. I will ask around and see where I can find the CIDs.

Ok never mind then, I'll pick the patch as-is.

author
#6 lore raw

PATCH 3/10 btrfs-progs: remove deadcode around metadump_v2 in check_chunk_refs

In reply to Eryu Guan

metadump_v2 is initialized to 0 and never updated again. So remove the deadcode.

---

I'm not sure about this one, seems metadump_v2 should be initialized depending on whether superblock has BTRFS_SUPER_FLAG_METADUMP_V2 flag set.

 cmds-check.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
cmds-check.c +1 -6
index 80a7c29..118a274 100644
@@ -7597,7 +7597,6 @@ static int check_chunk_refs(struct chunk_record *chunk_rec,
 	u64 devid;
 	u64 offset;
 	u64 length;
-	int metadump_v2 = 0;
 	int i;
 	int ret = 0;
 
@@ -7610,8 +7609,7 @@ static int check_chunk_refs(struct chunk_record *chunk_rec,
 					       cache);
 		if (chunk_rec->length != block_group_rec->offset ||
 		    chunk_rec->offset != block_group_rec->objectid ||
-		    (!metadump_v2 &&
-		     chunk_rec->type_flags != block_group_rec->flags)) {
+		    chunk_rec->type_flags != block_group_rec->flags) {
 			if (!silent)
 				fprintf(stderr,
 					"Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
@@ -7645,9 +7643,6 @@ static int check_chunk_refs(struct chunk_record *chunk_rec,
 		ret = 1;
 	}
 
-	if (metadump_v2)
-		return ret;
-
 	length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
 				    chunk_rec->num_stripes);
 	for (i = 0; i < chunk_rec->num_stripes; ++i) {
Signature · 1 line
2.4.3
#7 lore raw

Re: [PATCH 03/10] btrfs-progs: remove deadcode around metadump_v2 in check_chunk_refs

Quoted 2 lines · Eryu Guan

metadump_v2 is initialized to 0 and never updated again. So remove the deadcode.

Josef introduced the v2 metadump to resolve some issues with the v1 format. Even if the code appears dead, I think it's "not yet used", but I don't know what were the intentions with it.

author
#8 lore raw

PATCH 4/10 btrfs-progs: return -EIO properly in restore_metadump()

In reply to Eryu Guan

Error number -EIO is assigned to ret but later ret is overwritten by wait_for_worker().

---
 btrfs-image.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
btrfs-image.c +4 -1
index 82eed05..7d3a2f8 100644
@@ -2465,6 +2465,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
 	u64 bytenr = 0;
 	FILE *in = NULL;
 	int ret = 0;
+	int err = 0;
 
 	if (!strcmp(input, "-")) {
 		in = stdin;
@@ -2526,7 +2527,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
 		if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
 		    le64_to_cpu(header->bytenr) != bytenr) {
 			fprintf(stderr, "bad header in metadump image\n");
-			ret = -EIO;
+			err = -EIO;
 			break;
 		}
 		ret = add_cluster(cluster, &mdrestore, &bytenr);
@@ -2536,6 +2537,8 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
 		}
 	}
 	ret = wait_for_worker(&mdrestore);
+	if (!ret)
+		ret = err;
 
 	if (!ret && !multi_devices && !old_restore) {
 		struct btrfs_root *root;
Signature · 1 line
2.4.3
#9 lore raw

Re: [PATCH 04/10] btrfs-progs: return -EIO properly in restore_metadump()

Quoted 21 lines · Eryu Guan

Error number -EIO is assigned to ret but later ret is overwritten by wait_for_worker().

---
 btrfs-image.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
btrfs-image.c +1 -0
index 82eed05..7d3a2f8 100644
@@ -2465,6 +2465,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
 	u64 bytenr = 0;
 	FILE *in = NULL;
 	int ret = 0;
+	int err = 0;
 
 	if (!strcmp(input, "-")) {
 		in = stdin;

btrfs-image.c static int restore_metadump(const char *input, FILE *out, int old_restore, · line 2530

Quoted 5 lines
btrfs-image.c +1 -1
@@ -2526,7 +2527,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
 		if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
 		    le64_to_cpu(header->bytenr) != bytenr) {
 			fprintf(stderr, "bad header in metadump image\n");
-			ret = -EIO;
+			err = -EIO;

Shouldn't we do 'goto out' instead? As this looks likes the metadump is broken anyway so there's no point in continuing.

author
#10 lore raw

PATCH 5/10 btrfs-progs: mute coverity warnings about deadcode

In reply to Eryu Guan

Coverity reports execution cannot reach this statements. So put WARN_ON in if-else conditions.

---
 backref.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
backref.c +4 -4
index 9a2efca..8f41f82 100644
@@ -323,9 +323,9 @@ static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
 
 	eb = path->nodes[level];
 	while (!eb) {
-		WARN_ON(!level);
 		if (!level) {
 			ret = 1;
+			WARN_ON(1);
 			goto out;
 		}
 		level--;
@@ -1178,7 +1178,6 @@ int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
 		 logical, logical - found_key->objectid, found_key->objectid,
 		 found_key->offset, flags, item_size);
 
-	WARN_ON(!flags_ret);
 	if (flags_ret) {
 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
@@ -1187,9 +1186,10 @@ int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
 		else
 			BUG_ON(1);
 		return 0;
+	} else {
+		WARN_ON(1);
+		return -EIO;
 	}
-
-	return -EIO;
 }
 
 /*
Signature · 1 line
2.4.3
Applied
#11 lore raw

Re: [PATCH 05/10] btrfs-progs: mute coverity warnings about deadcode

Quoted 4 lines · Eryu Guan

Coverity reports execution cannot reach this statements. So put WARN_ON in if-else conditions.

Applied, thanks.

author
#12 lore raw

PATCH 6/10 btrfs-progs: vailidate pointer before dereferencing it in btrfs_cow_block()

In reply to Eryu Guan

Check trans before dereferencing it.

---
 ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ctree.c +1 -1
index 1434007..f9d972a 100644
@@ -356,7 +356,7 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans,
 		WARN_ON(1);
 	}
 	*/
-	if (trans->transid != root->fs_info->generation) {
+	if (trans && trans->transid != root->fs_info->generation) {
 		printk(KERN_CRIT "trans %llu running %llu\n",
 			(unsigned long long)trans->transid,
 			(unsigned long long)root->fs_info->generation);
Signature · 1 line
2.4.3
#13 lore raw

Re: [PATCH 06/10] btrfs-progs: vailidate pointer before dereferencing it in btrfs_cow_block()

Quoted 1 line · Eryu Guan

Check trans before dereferencing it.

Have you found a code path where 'trans' can be NULL? A quick search did not reveal anything.

author
#14 lore raw

PATCH 7/10 btrfs-progs: fix memory leak on error path

In reply to Eryu Guan

dev_scans and t_scans should be freed on malloc error.

---
 chunk-recover.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
chunk-recover.c +7 -2
index 1fb04f7..c727f0f 100644
@@ -847,11 +847,16 @@ static int scan_devices(struct recover_control *rc)
 	if (!dev_scans)
 		return -ENOMEM;
 	t_scans = (pthread_t *)malloc(sizeof(pthread_t) * devnr);
-	if (!t_scans)
+	if (!t_scans) {
+		free(dev_scans);
 		return -ENOMEM;
+	}
 	t_rets = (long *)malloc(sizeof(long) * devnr);
-	if (!t_rets)
+	if (!t_rets) {
+		free(dev_scans);
+		free(t_scans);
 		return -ENOMEM;
+	}
 
 	list_for_each_entry(dev, &rc->fs_devices->devices, dev_list) {
 		fd = open(dev->name, O_RDONLY);
Signature · 1 line
2.4.3
author
#15 lore raw

PATCH 8/10 btrfs-progs: remove identical branch in record_extent()

In reply to Eryu Guan

The same code is executed when the condition "ret" is true or false, because the code in the if-then branch and after the if statement is identical.

---
 cmds-check.c | 2 --
 1 file changed, 2 deletions(-)
cmds-check.c +0 -2
index 118a274..a459893 100644
@@ -6310,8 +6310,6 @@ static int record_extent(struct btrfs_trans_handle *trans,
 			"start %llu len %llu parent %llu root %llu\n",
 			rec->start, rec->max_size, parent, tback->root);
 	}
-	if (ret)
-		goto fail;
 fail:
 	btrfs_release_path(path);
 	return ret;
Signature · 1 line
2.4.3
author
#16 lore raw

PATCH 9/10 btrfs-progs: fix memory leak in cmd_qgroup_show()

In reply to Eryu Guan

filter_set and comparer_set should be freed on return.

---
 cmds-qgroup.c | 2 ++
 qgroup.c      | 1 +
 2 files changed, 3 insertions(+)
cmds-qgroup.c +2 -0
index 48c1733..82bd2e2 100644
@@ -354,6 +354,8 @@ static int cmd_qgroup_show(int argc, char **argv)
 	fd = open_file_or_dir(path, &dirstream);
 	if (fd < 0) {
 		fprintf(stderr, "ERROR: can't access '%s'\n", path);
+		btrfs_qgroup_free_filter_set(filter_set);
+		btrfs_qgroup_free_comparer_set(comparer_set);
 		return 1;
 	}
 
qgroup.c +1 -0
index ec9a3ac..0272aa8 100644
@@ -1211,6 +1211,7 @@ int btrfs_show_qgroups(int fd,
 
 	__free_all_qgroups(&qgroup_lookup);
 	btrfs_qgroup_free_filter_set(filter_set);
+	btrfs_qgroup_free_comparer_set(comp_set);
 	return ret;
 }
 
Signature · 1 line
2.4.3
author
#17 lore raw

PATCH 10/10 btrfs-progs: return -ENOMEM properly in btrfs_read_block_groups()

In reply to Eryu Guan

Breaking from the while loop makes ret overwritten to zero, goto error label directly and return -ENOMEM.

---
 extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
extent-tree.c +1 -1
index 0c8152a..97cf961 100644
@@ -3255,7 +3255,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
 		cache = kzalloc(sizeof(*cache), GFP_NOFS);
 		if (!cache) {
 			ret = -ENOMEM;
-			break;
+			goto error;
 		}
 
 		read_extent_buffer(leaf, &cache->item,
Signature · 1 line
2.4.3
#18 lore raw
In reply to Eryu Guan

I've merged 1, 2 (the first one), 7, 8, 9 and 10. Please add the missing coverity references and resend. The remaining patches seem unnecessary but please check whether I haven't missed something.