Skip to content

Commit f6a2906

Browse files
adambuchbinderkdave
authored andcommitted
btrfs-progs: Fix a use-after-free in the volumes code.
When a struct btrfs_fs_devices was being torn down by btrfs_close_devices(), there was an invalidated pointer in the global list fs_uuids which still pointed to it; if a device was closed and then reopened (which btrfs-convert does), freed memory would be accessed. This was found using ThreadSanitizer (pretty much doing what AddressSanitizer would, but not exiting after the first failure). To reproduce, build with -fsanitize=thread and run 'make test'. Representative output is below. This change makes the current tests TSan-clean. WARNING: ThreadSanitizer: heap-use-after-free (pid=29161) Read of size 8 at 0x7d180000eee0 by main thread: #0 memcmp ??:0 #1 find_fsid .../volumes.c:81 #2 device_list_add .../volumes.c:95 #3 btrfs_scan_one_device .../volumes.c:259 #4 btrfs_scan_fs_devices .../disk-io.c:1002 #5 __open_ctree_fd .../disk-io.c:1090 #6 open_ctree_fd .../disk-io.c:1191 #7 do_convert .../btrfs-convert.c:2317 #8 main .../btrfs-convert.c:2745 Previous write of size 8 at 0x7d180000eee0 by main thread: #0 free ??:0 #1 btrfs_close_devices .../volumes.c:191 #2 close_ctree .../disk-io.c:1401 #3 do_convert .../btrfs-convert.c:2300 #4 main .../btrfs-convert.c:2745 Location is heap block of size 96 at 0x7d180000eee0 allocated by main thread: #0 calloc ??:0 (exe+0x00000002acc6) #1 device_list_add .../volumes.c:97 #2 btrfs_scan_one_device .../volumes.c:259 #3 btrfs_scan_fs_devices .../disk-io.c:1002 #4 __open_ctree_fd .../disk-io.c:1090 #5 open_ctree_fd .../disk-io.c:1191 #6 do_convert .../btrfs-convert.c:2256 #7 main .../btrfs-convert.c:2745 Signed-off-by: Adam Buchbinder <[email protected]> Reviewed-by: Satoru Takeuchi <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent c9951e2 commit f6a2906

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

volumes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
188188
goto again;
189189
}
190190

191+
list_del(&fs_devices->list);
191192
free(fs_devices);
192193
return 0;
193194
}

volumes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct btrfs_device {
6767
struct btrfs_fs_devices {
6868
u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
6969

70-
/* the device with this id has the most recent coyp of the super */
70+
/* the device with this id has the most recent copy of the super */
7171
u64 latest_devid;
7272
u64 latest_trans;
7373
u64 lowest_devid;

0 commit comments

Comments
 (0)