Skip to contentSkip to conversation

btrfs: fix use-after-free of cmp workspace pages

PATCH In review Naohiro Aota 2 participants 2 messages Jul 13, 2018 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix use-after-free of cmp workspace pages

btrfs_cmp_data_free() puts cmp's src_pages and dst_pages, but leaves their page address intact. Now, if you hit "goto again" in btrfs_extent_same_range() and hit some error in btrfs_cmp_data_prepare(), you'll try to unlock/put already put pages.

This is simple fix to reset the address to avoid use-after-free.

Fixes 67b07bd4bec5 ("Btrfs: reuse cmp workspace in EXTENT_SAME ioctl")
---
 fs/btrfs/ioctl.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/ioctl.c +2 -0
index 43ecbe620dea..b077544b5232 100644
@@ -3327,11 +3327,13 @@ static void btrfs_cmp_data_free(struct cmp_pages *cmp)
 		if (pg) {
 			unlock_page(pg);
 			put_page(pg);
+			cmp->src_pages[i] = NULL;
 		}
 		pg = cmp->dst_pages[i];
 		if (pg) {
 			unlock_page(pg);
 			put_page(pg);
+			cmp->dst_pages[i] = NULL;
 		}
 	}
 }
Signature · 1 line
2.18.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Naohiro Aota

btrfs_cmp_data_free() puts cmp's src_pages and dst_pages, but leaves their page address intact. Now, if you hit "goto again" in btrfs_extent_same_range() and hit some error in btrfs_cmp_data_prepare(), you'll try to unlock/put already put pages.

This is simple fix to reset the address to avoid use-after-free.

Fixes 67b07bd4bec5 ("Btrfs: reuse cmp workspace in EXTENT_SAME ioctl")

Thanks for catching it.