Filipe Manana
580c079b57
btrfs: fix double free on ulist after backref resolution failure
...
At btrfs_find_all_roots_safe() we allocate a ulist and set the **roots
argument to point to it. However if later we fail due to an error returned
by find_parent_nodes(), we free that ulist but leave a dangling pointer in
the **roots argument. Upon receiving the error, a caller of this function
can attempt to free the same ulist again, resulting in an invalid memory
access.
One such scenario is during qgroup accounting:
btrfs_qgroup_account_extents()
--> calls btrfs_find_all_roots() passes &new_roots (a stack allocated
pointer) to btrfs_find_all_roots()
--> btrfs_find_all_roots() just calls btrfs_find_all_roots_safe()
passing &new_roots to it
--> allocates ulist and assigns its address to **roots (which
points to new_roots from btrfs_qgroup_account_extents())
--> find_parent_nodes() returns an error, so we free the ulist
and leave **roots pointing to it after returning
--> btrfs_qgroup_account_extents() sees btrfs_find_all_roots() returned
an error and jumps to the label 'cleanup', which just tries to
free again the same ulist
Stack trace example:
------------[ cut here ]------------
BTRFS: tree first key check failed
WARNING: CPU: 1 PID: 1763215 at fs/btrfs/disk-io.c:422 btrfs_verify_level_key+0xe0/0x180 [btrfs]
Modules linked in: dm_snapshot dm_thin_pool (...)
CPU: 1 PID: 1763215 Comm: fsstress Tainted: G W 5.8.0-rc3-btrfs-next-64 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.13.0-0-gf21b5a4aeb02-prebuilt.qemu.org 04/01/2014
RIP: 0010:btrfs_verify_level_key+0xe0/0x180 [btrfs]
Code: 28 5b 5d (...)
RSP: 0018:ffffb89b473779a0 EFLAGS: 00010286
RAX: 0000000000000000 RBX: ffff90397759bf08 RCX: 0000000000000000
RDX: 0000000000000001 RSI: 0000000000000027 RDI: 00000000ffffffff
RBP: ffff9039a419c000 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: ffffb89b43301000 R12: 000000000000005e
R13: ffffb89b47377a2e R14: ffffb89b473779af R15: 0000000000000000
FS: 00007fc47e1e1000(0000) GS:ffff9039ac200000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fc47e1df000 CR3: 00000003d9e4e001 CR4: 00000000003606e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
read_block_for_search+0xf6/0x350 [btrfs]
btrfs_next_old_leaf+0x242/0x650 [btrfs]
resolve_indirect_refs+0x7cf/0x9e0 [btrfs]
find_parent_nodes+0x4ea/0x12c0 [btrfs]
btrfs_find_all_roots_safe+0xbf/0x130 [btrfs]
btrfs_qgroup_account_extents+0x9d/0x390 [btrfs]
btrfs_commit_transaction+0x4f7/0xb20 [btrfs]
btrfs_sync_file+0x3d4/0x4d0 [btrfs]
do_fsync+0x38/0x70
__x64_sys_fdatasync+0x13/0x20
do_syscall_64+0x5c/0xe0
entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x7fc47e2d72e3
Code: Bad RIP value.
RSP: 002b:00007fffa32098c8 EFLAGS: 00000246 ORIG_RAX: 000000000000004b
RAX: ffffffffffffffda RBX: 0000000000000003 RCX: 00007fc47e2d72e3
RDX: 00007fffa3209830 RSI: 00007fffa3209830 RDI: 0000000000000003
RBP: 000000000000072e R08: 0000000000000001 R09: 0000000000000003
R10: 0000000000000000 R11: 0000000000000246 R12: 00000000000003e8
R13: 0000000051eb851f R14: 00007fffa3209970 R15: 00005607c4ac8b50
irq event stamp: 0
hardirqs last enabled at (0): [<0000000000000000>] 0x0
hardirqs last disabled at (0): [<ffffffffb8eb5e85>] copy_process+0x755/0x1eb0
softirqs last enabled at (0): [<ffffffffb8eb5e85>] copy_process+0x755/0x1eb0
softirqs last disabled at (0): [<0000000000000000>] 0x0
---[ end trace 8639237550317b48 ]---
BTRFS error (device sdc): tree first key mismatch detected, bytenr=62324736 parent_transid=94 key expected=(262,108,1351680) has=(259,108,1921024)
general protection fault, probably for non-canonical address 0x6b6b6b6b6b6b6b6b: 0000 [#1 ] PREEMPT SMP DEBUG_PAGEALLOC PTI
CPU: 2 PID: 1763215 Comm: fsstress Tainted: G W 5.8.0-rc3-btrfs-next-64 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.13.0-0-gf21b5a4aeb02-prebuilt.qemu.org 04/01/2014
RIP: 0010:ulist_release+0x14/0x60 [btrfs]
Code: c7 07 00 (...)
RSP: 0018:ffffb89b47377d60 EFLAGS: 00010282
RAX: 6b6b6b6b6b6b6b6b RBX: ffff903959b56b90 RCX: 0000000000000000
RDX: 0000000000000001 RSI: 0000000000270024 RDI: ffff9036e2adc840
RBP: ffff9036e2adc848 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: ffff9036e2adc840
R13: 0000000000000015 R14: ffff9039a419ccf8 R15: ffff90395d605840
FS: 00007fc47e1e1000(0000) GS:ffff9039ac600000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f8c1c0a51c8 CR3: 00000003d9e4e004 CR4: 00000000003606e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
ulist_free+0x13/0x20 [btrfs]
btrfs_qgroup_account_extents+0xf3/0x390 [btrfs]
btrfs_commit_transaction+0x4f7/0xb20 [btrfs]
btrfs_sync_file+0x3d4/0x4d0 [btrfs]
do_fsync+0x38/0x70
__x64_sys_fdatasync+0x13/0x20
do_syscall_64+0x5c/0xe0
entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x7fc47e2d72e3
Code: Bad RIP value.
RSP: 002b:00007fffa32098c8 EFLAGS: 00000246 ORIG_RAX: 000000000000004b
RAX: ffffffffffffffda RBX: 0000000000000003 RCX: 00007fc47e2d72e3
RDX: 00007fffa3209830 RSI: 00007fffa3209830 RDI: 0000000000000003
RBP: 000000000000072e R08: 0000000000000001 R09: 0000000000000003
R10: 0000000000000000 R11: 0000000000000246 R12: 00000000000003e8
R13: 0000000051eb851f R14: 00007fffa3209970 R15: 00005607c4ac8b50
Modules linked in: dm_snapshot dm_thin_pool (...)
---[ end trace 8639237550317b49 ]---
RIP: 0010:ulist_release+0x14/0x60 [btrfs]
Code: c7 07 00 (...)
RSP: 0018:ffffb89b47377d60 EFLAGS: 00010282
RAX: 6b6b6b6b6b6b6b6b RBX: ffff903959b56b90 RCX: 0000000000000000
RDX: 0000000000000001 RSI: 0000000000270024 RDI: ffff9036e2adc840
RBP: ffff9036e2adc848 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: ffff9036e2adc840
R13: 0000000000000015 R14: ffff9039a419ccf8 R15: ffff90395d605840
FS: 00007fc47e1e1000(0000) GS:ffff9039ad200000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f6a776f7d40 CR3: 00000003d9e4e002 CR4: 00000000003606e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Fix this by making btrfs_find_all_roots_safe() set *roots to NULL after
it frees the ulist.
Fixes: 8da6d5815c592b ("Btrfs: added btrfs_find_all_roots()")
CC: stable@vger.kernel.org # 4.4+
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-07-21 21:59:15 +02:00
..
2020-03-27 09:29:56 +00:00
2020-01-25 11:31:59 -05:00
2019-11-18 14:26:43 +01:00
2020-05-23 17:16:18 -07:00
2020-03-13 21:08:17 -04:00
2019-08-30 07:27:17 -07:00
2019-08-30 07:27:17 -07:00
2020-07-21 21:59:15 +02:00
2020-05-08 23:01:10 +01:00
2020-05-10 11:16:07 -07:00
2020-05-14 17:47:01 -05:00
2019-09-19 09:42:37 -07:00
2020-04-27 08:17:10 +02:00
2020-02-07 14:48:41 -05:00
2020-03-31 12:58:36 -07:00
2020-04-17 17:08:50 +02:00
2019-09-03 09:30:56 -04:00
2019-12-18 18:07:31 +01:00
2020-02-17 21:08:37 -08:00
2020-02-23 21:59:42 +01:00
2019-08-30 07:27:17 -07:00
2020-03-03 23:40:52 +08:00
2020-05-21 16:40:11 -07:00
2019-11-11 09:21:59 -05:00
2020-03-23 13:01:37 +01:00
2020-05-19 15:03:37 -04:00
2020-04-07 13:48:26 -07:00
2020-03-06 07:06:09 -06:00
2019-08-30 07:27:17 -07:00
2020-02-04 03:05:26 +00:00
2020-02-13 09:16:07 +01:00
2020-05-08 15:01:25 -05:00
2019-12-18 18:07:32 +01:00
2020-04-10 15:36:20 -07:00
2020-03-29 23:23:00 +02:00
2019-10-23 17:23:46 +02:00
2020-04-02 09:35:32 -07:00
2020-05-25 13:12:53 +02:00
2019-09-19 09:42:37 -07:00
2020-03-05 20:25:05 -05:00
2020-02-07 14:48:37 -05:00
2020-02-05 05:28:20 +00:00
2020-03-16 15:53:47 -04:00
2020-02-04 03:05:26 +00:00
2019-08-30 07:27:17 -07:00
2020-05-14 18:42:44 -04:00
2019-05-21 10:50:46 +02:00
2020-04-17 12:40:31 -04:00
2019-10-23 17:23:46 +02:00
2019-05-21 10:50:46 +02:00
2020-03-30 12:40:53 +02:00
2020-03-28 13:21:08 +01:00
2020-04-23 13:45:27 -04:00
2019-08-30 08:11:25 -07:00
2019-07-19 10:42:02 -07:00
2020-04-08 09:39:11 -04:00
2020-05-13 11:10:57 +02:00
2020-04-25 12:25:32 -07:00
2020-03-09 14:45:40 -07:00
2019-08-30 07:27:17 -07:00
2019-08-30 07:27:17 -07:00
2020-01-30 15:37:41 -08:00
2020-02-07 14:48:37 -05:00
2020-04-07 10:43:44 -07:00
2019-09-19 10:06:57 -07:00
2019-09-19 10:06:57 -07:00
2020-04-05 11:34:35 -07:00
2019-08-30 07:27:18 -07:00
2019-12-10 22:29:58 -05:00
2020-05-17 23:38:21 +02:00
2020-04-10 15:36:22 -07:00
2019-09-19 09:42:37 -07:00
2020-03-25 11:50:48 +01:00
2020-05-07 08:45:47 -06:00
2020-01-14 13:28:28 -08:00
2020-04-16 09:07:42 -07:00
2020-03-25 11:28:26 +09:00
2020-02-03 17:27:47 -07:00
2019-07-19 10:42:02 -07:00
2019-12-08 19:10:50 -05:00
2019-05-21 10:50:45 +02:00
2019-11-15 14:38:29 +01:00
2020-04-07 10:43:44 -07:00
2019-05-21 10:50:45 +02:00
2019-07-16 19:23:22 -07:00
2019-07-19 10:42:02 -07:00
2019-05-21 10:50:45 +02:00
2020-04-21 09:51:10 -06:00
2020-04-24 12:44:19 -07:00
2020-01-06 20:10:26 +01:00
2019-11-15 14:38:29 +01:00
2019-06-19 17:09:55 +02:00
2020-04-28 17:54:13 +02:00
2019-08-30 19:31:09 -04:00
2020-04-02 19:15:03 -07:00
2019-12-08 11:08:28 -08:00
2019-05-21 10:50:45 +02:00
2020-06-09 19:23:18 +02:00
2019-12-18 00:03:01 -05:00
2020-02-03 17:27:38 -07:00
2020-05-14 10:00:35 -07:00
2020-05-17 10:48:24 -05:00
2020-03-03 10:55:06 -05:00
2019-08-07 21:51:47 -04:00
2019-08-19 11:00:39 -04:00
2020-05-19 18:29:36 -04:00
2020-04-10 15:36:22 -07:00
2020-02-07 14:48:35 -05:00
2020-04-02 09:35:26 -07:00
2019-07-16 22:52:37 -04:00
2019-05-21 10:50:45 +02:00
2020-01-31 10:30:36 -08:00
2020-02-07 14:48:35 -05:00
2020-03-06 11:06:15 +01:00
2020-04-02 12:30:08 -07:00
2020-05-20 07:26:47 -06:00
2020-04-03 11:35:57 -06:00
2020-04-03 11:35:57 -06:00
2020-04-30 07:57:46 -07:00
2020-03-05 21:00:40 -05:00
2019-06-24 09:16:47 +10:00
2020-03-24 13:27:16 +01:00
2020-03-18 13:03:38 -07:00
2020-03-05 21:00:40 -05:00
2019-05-21 10:50:45 +02:00
2019-07-16 22:52:37 -04:00
2020-01-09 08:16:12 -07:00
2020-04-06 10:38:59 -04:00
2020-03-13 21:08:17 -04:00
2019-05-30 11:26:32 -07:00
2020-03-12 17:33:11 -07:00
2020-04-02 12:30:08 -07:00
2020-04-02 09:35:28 -07:00
2020-04-27 10:37:14 -04:00
2019-05-30 11:29:53 -07:00
2020-01-04 13:55:09 -08:00
2019-09-06 21:28:49 +02:00
2020-04-03 00:09:59 +11:00
2020-01-23 10:15:28 -08:00
2019-11-15 14:38:29 +01:00
2020-04-10 15:36:22 -07:00
2020-05-20 10:54:29 -07:00
2019-12-08 14:37:36 +01:00
2020-01-20 17:03:54 -07:00
2019-10-03 14:21:35 -07:00
2020-04-28 14:37:40 -07:00
2019-05-14 09:47:50 -07:00
2020-01-14 12:20:53 +01:00
2020-04-07 10:43:40 -07:00
2019-12-08 19:10:50 -05:00
2020-03-16 15:53:47 -04:00