Skip to contentSkip to conversation

btrfs: prevent memory leak

PATCH NAK Navid Emamdoost 2 participants 2 messages Sep 23, 2019 – Sep 24, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: prevent memory leak

In btrfsic_mount if btrfsic_dev_state_alloc fails the allocated state will be leaked. It needs to be released on error handling path.

---
 fs/btrfs/check-integrity.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/check-integrity.c +1 -0
index 0b52ab4cb964..8a77b0cb2db3 100644
@@ -2941,6 +2941,7 @@ int btrfsic_mount(struct btrfs_fs_info *fs_info,
 		if (NULL == ds) {
 			pr_info("btrfs check-integrity: kmalloc() failed!\n");
 			mutex_unlock(&btrfsic_mutex);
+			kvfree(state);
 			return -ENOMEM;
 		}
 		ds->bdev = device->bdev;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 4 lines · Navid Emamdoost

In btrfsic_mount if btrfsic_dev_state_alloc fails the allocated state will be leaked. It needs to be released on error handling path.

NAK.

The allocated state could have been added to a btrfsic_dev_state which in turn is linked by btrfsic_dev_state_hashtable_add. So when later ds->state is dereferenced it will case a UAF and likely result in crash.

Looking at the integrity code I also don't like how it's structured e.g. if a memory error occurs then only some devices will be added to the btrfsic_dev_State_hashtable and the super block is not going to be validated at all.

Quoted 17 lines
---
 fs/btrfs/check-integrity.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/check-integrity.c +1 -0
index 0b52ab4cb964..8a77b0cb2db3 100644
@@ -2941,6 +2941,7 @@ int btrfsic_mount(struct btrfs_fs_info *fs_info,
 		if (NULL == ds) {
 			pr_info("btrfs check-integrity: kmalloc() failed!\n");
 			mutex_unlock(&btrfsic_mutex);
+			kvfree(state);
 			return -ENOMEM;
 		}
 		ds->bdev = device->bdev;